about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--clippy_lints/src/methods/mod.rs49
1 files changed, 22 insertions, 27 deletions
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs
index 2311d0df764..431053846f7 100644
--- a/clippy_lints/src/methods/mod.rs
+++ b/clippy_lints/src/methods/mod.rs
@@ -2172,42 +2172,37 @@ fn lint_flat_map_identity<'a, 'tcx>(
     expr: &'tcx hir::Expr,
     flat_map_args: &'tcx [hir::Expr],
 ) {
-    if_chain! {
-        if match_trait_method(cx, expr, &paths::ITERATOR);
+    if match_trait_method(cx, expr, &paths::ITERATOR) {
+        let arg_node = &flat_map_args[1].node;
 
-        if flat_map_args.len() == 2;
+        let apply_lint = |message: &str| {
+            span_lint(cx, FLAT_MAP_IDENTITY, expr.span, message);
+        };
 
-        then {
-            if_chain! {
-                if let hir::ExprKind::Closure(_, _, body_id, _, _) = flat_map_args[1].node;
-                let body = cx.tcx.hir().body(body_id);
+        if_chain! {
+            if let hir::ExprKind::Closure(_, _, body_id, _, _) = arg_node;
+            let body = cx.tcx.hir().body(*body_id);
 
-                if body.arguments.len() == 1;
-                if let hir::PatKind::Binding(_, _, binding_ident, _) = body.arguments[0].pat.node;
-                if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = body.value.node;
+            if let hir::PatKind::Binding(_, _, binding_ident, _) = body.arguments[0].pat.node;
+            if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = body.value.node;
 
-                if path.segments.len() == 1;
-                if path.segments[0].ident.as_str() == binding_ident.as_str();
+            if path.segments.len() == 1;
+            if path.segments[0].ident.as_str() == binding_ident.as_str();
 
-                then {
-                    let msg = "called `flat_map(|x| x)` on an `Iterator`. \
-                               This can be simplified by calling `flatten().`";
-                    span_lint(cx, FLAT_MAP_IDENTITY, expr.span, msg);
-                }
+            then {
+                apply_lint("called `flat_map(|x| x)` on an `Iterator`. \
+                            This can be simplified by calling `flatten().`");
             }
+        }
 
-            if_chain! {
-                let expr = &flat_map_args[1];
-
-                if let hir::ExprKind::Path(ref qpath) = expr.node;
+        if_chain! {
+            if let hir::ExprKind::Path(ref qpath) = arg_node;
 
-                if match_qpath(qpath, &paths::STD_CONVERT_IDENTITY);
+            if match_qpath(qpath, &paths::STD_CONVERT_IDENTITY);
 
-                then {
-                    let msg = "called `flat_map(std::convert::identity)` on an `Iterator`. \
-                               This can be simplified by calling `flatten().`";
-                    span_lint(cx, FLAT_MAP_IDENTITY, expr.span, msg);
-                }
+            then {
+                apply_lint("called `flat_map(std::convert::identity)` on an `Iterator`. \
+                            This can be simplified by calling `flatten().`");
             }
         }
     }