about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--clippy_lints/src/methods/mod.rs18
-rw-r--r--tests/ui/unnecessary_flat_map.rs2
-rw-r--r--tests/ui/unnecessary_flat_map.stderr12
3 files changed, 21 insertions, 11 deletions
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs
index 431053846f7..b69534a7703 100644
--- a/clippy_lints/src/methods/mod.rs
+++ b/clippy_lints/src/methods/mod.rs
@@ -2176,7 +2176,17 @@ fn lint_flat_map_identity<'a, 'tcx>(
         let arg_node = &flat_map_args[1].node;
 
         let apply_lint = |message: &str| {
-            span_lint(cx, FLAT_MAP_IDENTITY, expr.span, message);
+            if let hir::ExprKind::MethodCall(_, span, _) = &expr.node {
+                span_lint_and_sugg(
+                    cx,
+                    FLAT_MAP_IDENTITY,
+                    *span,
+                    message,
+                    "try",
+                    "flatten()".to_string(),
+                    Applicability::MachineApplicable,
+                );
+            }
         };
 
         if_chain! {
@@ -2190,8 +2200,7 @@ fn lint_flat_map_identity<'a, 'tcx>(
             if path.segments[0].ident.as_str() == binding_ident.as_str();
 
             then {
-                apply_lint("called `flat_map(|x| x)` on an `Iterator`. \
-                            This can be simplified by calling `flatten().`");
+                apply_lint("called `flat_map(|x| x)` on an `Iterator`");
             }
         }
 
@@ -2201,8 +2210,7 @@ fn lint_flat_map_identity<'a, 'tcx>(
             if match_qpath(qpath, &paths::STD_CONVERT_IDENTITY);
 
             then {
-                apply_lint("called `flat_map(std::convert::identity)` on an `Iterator`. \
-                            This can be simplified by calling `flatten().`");
+                apply_lint("called `flat_map(std::convert::identity)` on an `Iterator`");
             }
         }
     }
diff --git a/tests/ui/unnecessary_flat_map.rs b/tests/ui/unnecessary_flat_map.rs
index 955e791dd2b..32eaef4754f 100644
--- a/tests/ui/unnecessary_flat_map.rs
+++ b/tests/ui/unnecessary_flat_map.rs
@@ -1,3 +1,5 @@
+// run-rustfix
+
 #![warn(clippy::flat_map_identity)]
 
 use std::convert;
diff --git a/tests/ui/unnecessary_flat_map.stderr b/tests/ui/unnecessary_flat_map.stderr
index 4872e37f324..968a1e01026 100644
--- a/tests/ui/unnecessary_flat_map.stderr
+++ b/tests/ui/unnecessary_flat_map.stderr
@@ -1,16 +1,16 @@
-error: called `flat_map(|x| x)` on an `Iterator`. This can be simplified by calling `flatten().`
-  --> $DIR/unnecessary_flat_map.rs:7:5
+error: called `flat_map(|x| x)` on an `Iterator`
+  --> $DIR/unnecessary_flat_map.rs:9:14
    |
 LL |     iterator.flat_map(|x| x);
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^
+   |              ^^^^^^^^^^^^^^^ help: try: `flatten()`
    |
    = note: `-D clippy::flat-map-identity` implied by `-D warnings`
 
-error: called `flat_map(std::convert::identity)` on an `Iterator`. This can be simplified by calling `flatten().`
-  --> $DIR/unnecessary_flat_map.rs:10:23
+error: called `flat_map(std::convert::identity)` on an `Iterator`
+  --> $DIR/unnecessary_flat_map.rs:12:14
    |
 LL |     iterator.flat_map(convert::identity);
-   |                       ^^^^^^^^^^^^^^^^^
+   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `flatten()`
 
 error: aborting due to 2 previous errors