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,25 +2041,24 @@ 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 { let arg_ty = cx.typeck_results().expr_ty(arg);
ExprKind::AddrOf(_, _, arg_expr) => { 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, "..")) format!("{}.iter().flatten()", snippet(cx, arg_expr.span, ".."))
}, } else {
ExprKind::MethodCall(_, _, _, _) | ExprKind::Path(QPath::Resolved(None, _)) => { return;
// Determine if `arg` is `Iterator` or implicitly calls `into_iter` }
let arg_ty = cx.typeck_results().expr_ty(arg); } else if let Some(id) = get_trait_def_id(cx, &paths::ITERATOR) {
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) } else {
} else { format!("{}.into_iter().flatten()", arg_snippet)
format!("{}.into_iter().flatten()", arg_snippet) }
} } else {
} else { return
return
}
},
_ => return,
}; };
span_lint_and_sugg( span_lint_and_sugg(