Skip to content

Commit 7439ecb

Browse files
Added check for type unification with the iter
1 parent b31625c commit 7439ecb

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

clippy_lints/src/methods/iter_on_single_or_empty_collections.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,18 @@ fn is_arg_ty_unified_in_fn<'tcx>(
3737
) -> bool {
3838
let fn_sig = cx.tcx.fn_sig(fn_id).instantiate_identity();
3939
let arg_id_in_args = args.into_iter().position(|e| e.hir_id == arg_id).unwrap();
40-
let arg_ty_in_args = fn_sig.input(arg_id_in_args);
40+
let arg_ty_in_args = fn_sig.input(arg_id_in_args).skip_binder();
4141

4242
cx.tcx.predicates_of(fn_id).predicates.iter().any(|(clause, _)| {
4343
clause
4444
.as_projection_clause()
4545
.and_then(|p| p.map_bound(|p| p.term.ty()).transpose())
46-
.is_some_and(|ty| ty == arg_ty_in_args)
47-
})
46+
.is_some_and(|ty| ty.skip_binder() == arg_ty_in_args)
47+
}) || fn_sig
48+
.inputs()
49+
.iter()
50+
.enumerate()
51+
.any(|(i, ty)| i != arg_id_in_args && ty.skip_binder().walk().any(|arg| arg.as_type() == Some(arg_ty_in_args)))
4852
}
4953

5054
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, method_name: &str, recv: &'tcx Expr<'tcx>) {

tests/ui/iter_on_empty_collections.fixed

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ fn array() {
3838
for i in Option::map_or(smth.as_ref(), [].iter(), |s| s.iter()) {
3939
println!("{i}");
4040
}
41+
42+
// Same as above, but when there are no predicates that mention the collection iter type.
43+
let mut iter = [34, 228, 35].iter();
44+
let _ = std::mem::replace(&mut iter, [].iter());
4145
}
4246

4347
macro_rules! in_macros {

tests/ui/iter_on_empty_collections.rs

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ fn array() {
3838
for i in Option::map_or(smth.as_ref(), [].iter(), |s| s.iter()) {
3939
println!("{i}");
4040
}
41+
42+
// Same as above, but when there are no predicates that mention the collection iter type.
43+
let mut iter = [34, 228, 35].iter();
44+
let _ = std::mem::replace(&mut iter, [].iter());
4145
}
4246

4347
macro_rules! in_macros {

0 commit comments

Comments
 (0)