mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-28 07:30:57 +00:00
Add additional check on if arg type has iter method
This commit is contained in:
parent
e07cd5b6fe
commit
0f5e71f8f2
1 changed files with 17 additions and 18 deletions
|
@ -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(
|
||||||
|
|
Loading…
Reference in a new issue