Skip to content

Commit 0f5e71f

Browse files
committed
Add additional check on if arg type has iter method
1 parent e07cd5b commit 0f5e71f

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

clippy_lints/src/loops.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,25 +2041,24 @@ fn check_manual_flatten<'tcx>(
20412041
&mut applicability,
20422042
);
20432043
// Determine if `arg` is by reference, an `Iterator`, or implicitly adjusted with `into_iter`
2044-
let hint = match arg.kind {
2045-
ExprKind::AddrOf(_, _, arg_expr) => {
2044+
let arg_ty = cx.typeck_results().expr_ty(arg);
2045+
let hint = if arg_ty.is_ref() {
2046+
if has_iter_method(cx, arg_ty).is_none() {
2047+
return;
2048+
} else if let ExprKind::AddrOf(_, _, arg_expr) = arg.kind {
20462049
format!("{}.iter().flatten()", snippet(cx, arg_expr.span, ".."))
2047-
},
2048-
ExprKind::MethodCall(_, _, _, _) | ExprKind::Path(QPath::Resolved(None, _)) => {
2049-
// Determine if `arg` is `Iterator` or implicitly calls `into_iter`
2050-
let arg_ty = cx.typeck_results().expr_ty(arg);
2051-
if let Some(id) = get_trait_def_id(cx, &paths::ITERATOR) {
2052-
let is_iterator = implements_trait(cx, arg_ty, id, &[]);
2053-
if is_iterator {
2054-
format!("{}.flatten()", arg_snippet)
2055-
} else {
2056-
format!("{}.into_iter().flatten()", arg_snippet)
2057-
}
2058-
} else {
2059-
return
2060-
}
2061-
},
2062-
_ => return,
2050+
} else {
2051+
return;
2052+
}
2053+
} else if let Some(id) = get_trait_def_id(cx, &paths::ITERATOR) {
2054+
let is_iterator = implements_trait(cx, arg_ty, id, &[]);
2055+
if is_iterator {
2056+
format!("{}.flatten()", arg_snippet)
2057+
} else {
2058+
format!("{}.into_iter().flatten()", arg_snippet)
2059+
}
2060+
} else {
2061+
return
20632062
};
20642063

20652064
span_lint_and_sugg(

0 commit comments

Comments
 (0)