Skip to content

Commit b31625c

Browse files
Accounted for possible extra layers before the consuming parent expr
1 parent 3955bd4 commit b31625c

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

clippy_lints/src/methods/iter_on_single_or_empty_collections.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, method
7777
.qpath_res(path, *hir_id)
7878
.opt_def_id()
7979
.filter(|fn_id| cx.tcx.def_kind(fn_id).is_fn_like())
80-
.is_some_and(|fn_id| is_arg_ty_unified_in_fn(cx, fn_id, expr.hir_id, args)),
80+
.is_some_and(|fn_id| is_arg_ty_unified_in_fn(cx, fn_id, child_id, args)),
8181
ExprKind::MethodCall(_name, recv, args, _span) => is_arg_ty_unified_in_fn(
8282
cx,
8383
cx.typeck_results().type_dependent_def_id(parent.hir_id).unwrap(),
84-
expr.hir_id,
84+
child_id,
8585
once(recv).chain(args.iter()),
8686
),
8787
ExprKind::If(_, _, _)

tests/ui/iter_on_empty_collections.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ fn array() {
2929
println!("{i}");
3030
}
3131

32+
// Same as above, but for empty collection iters with extra layers
33+
for i in smth.as_ref().map_or({ [].iter() }, |s| s.iter()) {
34+
println!("{y}", y = i + 1);
35+
}
36+
3237
// Same as above, but for regular function calls
3338
for i in Option::map_or(smth.as_ref(), [].iter(), |s| s.iter()) {
3439
println!("{i}");

tests/ui/iter_on_empty_collections.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ fn array() {
2929
println!("{i}");
3030
}
3131

32+
// Same as above, but for empty collection iters with extra layers
33+
for i in smth.as_ref().map_or({ [].iter() }, |s| s.iter()) {
34+
println!("{y}", y = i + 1);
35+
}
36+
3237
// Same as above, but for regular function calls
3338
for i in Option::map_or(smth.as_ref(), [].iter(), |s| s.iter()) {
3439
println!("{i}");

0 commit comments

Comments
 (0)