Add additional check on if arg type has iter method

This commit is contained in:
nahuakang 2021-02-03 09:39:35 +01:00
parent e07cd5b6fe
commit 0f5e71f8f2

View file

@ -2041,14 +2041,16 @@ fn check_manual_flatten<'tcx>(
&mut applicability, &mut applicability,
); );
// Determine if `arg` is by reference, an `Iterator`, or implicitly adjusted with `into_iter` // Determine if `arg` is by reference, an `Iterator`, or implicitly adjusted with `into_iter`
let hint = match arg.kind {
ExprKind::AddrOf(_, _, arg_expr) => {
format!("{}.iter().flatten()", snippet(cx, arg_expr.span, ".."))
},
ExprKind::MethodCall(_, _, _, _) | ExprKind::Path(QPath::Resolved(None, _)) => {
// Determine if `arg` is `Iterator` or implicitly calls `into_iter`
let arg_ty = cx.typeck_results().expr_ty(arg); let arg_ty = cx.typeck_results().expr_ty(arg);
if let Some(id) = get_trait_def_id(cx, &paths::ITERATOR) { let hint = if arg_ty.is_ref() {
if has_iter_method(cx, arg_ty).is_none() {
return;
} else if let ExprKind::AddrOf(_, _, arg_expr) = arg.kind {
format!("{}.iter().flatten()", snippet(cx, arg_expr.span, ".."))
} else {
return;
}
} else if let Some(id) = get_trait_def_id(cx, &paths::ITERATOR) {
let is_iterator = implements_trait(cx, arg_ty, id, &[]); let is_iterator = implements_trait(cx, arg_ty, id, &[]);
if is_iterator { if is_iterator {
format!("{}.flatten()", arg_snippet) format!("{}.flatten()", arg_snippet)
@ -2057,9 +2059,6 @@ fn check_manual_flatten<'tcx>(
} }
} else { } else {
return return
}
},
_ => return,
}; };
span_lint_and_sugg( span_lint_and_sugg(