From 1beb61c830ced8204cced04a531ddf270cdf5d50 Mon Sep 17 00:00:00 2001
From: Philipp Hansch <dev@phansch.net>
Date: Wed, 25 Sep 2019 07:48:18 +0200
Subject: [PATCH] Add run-rustfix for result_map_unit_fn lint

---
 tests/ui/result_map_unit_fn.fixed             |  73 ++++++++++++
 tests/ui/result_map_unit_fn.rs                |  35 +-----
 tests/ui/result_map_unit_fn.stderr            | 112 +++---------------
 tests/ui/result_map_unit_fn_non_rustfix.rs    |  74 ++++++++++++
 .../ui/result_map_unit_fn_non_rustfix.stderr  | 102 ++++++++++++++++
 5 files changed, 269 insertions(+), 127 deletions(-)
 create mode 100644 tests/ui/result_map_unit_fn.fixed
 create mode 100644 tests/ui/result_map_unit_fn_non_rustfix.rs
 create mode 100644 tests/ui/result_map_unit_fn_non_rustfix.stderr

diff --git a/tests/ui/result_map_unit_fn.fixed b/tests/ui/result_map_unit_fn.fixed
new file mode 100644
index 000000000000..d5245853e886
--- /dev/null
+++ b/tests/ui/result_map_unit_fn.fixed
@@ -0,0 +1,73 @@
+// run-rustfix
+// rustfix-only-machine-applicable
+
+#![feature(never_type)]
+#![warn(clippy::result_map_unit_fn)]
+#![allow(unused)]
+
+fn do_nothing<T>(_: T) {}
+
+fn diverge<T>(_: T) -> ! {
+    panic!()
+}
+
+fn plus_one(value: usize) -> usize {
+    value + 1
+}
+
+struct HasResult {
+    field: Result<usize, usize>,
+}
+
+impl HasResult {
+    fn do_result_nothing(self: &Self, value: usize) {}
+
+    fn do_result_plus_one(self: &Self, value: usize) -> usize {
+        value + 1
+    }
+}
+
+#[rustfmt::skip]
+fn result_map_unit_fn() {
+    let x = HasResult { field: Ok(10) };
+
+    let captured = 10;
+    if let Ok(value) = x.field { do_nothing(value + captured) };
+    let _: Result<(), usize> = x.field.map(|value| do_nothing(value + captured));
+
+    if let Ok(value) = x.field { x.do_result_nothing(value + captured) }
+
+    if let Ok(value) = x.field { x.do_result_plus_one(value + captured); }
+
+
+    if let Ok(value) = x.field { do_nothing(value + captured) }
+
+    if let Ok(value) = x.field { do_nothing(value + captured) }
+
+    if let Ok(value) = x.field { do_nothing(value + captured); }
+
+    if let Ok(value) = x.field { do_nothing(value + captured); }
+
+
+    if let Ok(value) = x.field { diverge(value + captured) }
+
+    if let Ok(value) = x.field { diverge(value + captured) }
+
+    if let Ok(value) = x.field { diverge(value + captured); }
+
+    if let Ok(value) = x.field { diverge(value + captured); }
+
+
+    x.field.map(|value| plus_one(value + captured));
+    x.field.map(|value| { plus_one(value + captured) });
+    if let Ok(value) = x.field { let y = plus_one(value + captured); }
+
+    if let Ok(value) = x.field { plus_one(value + captured); }
+
+    if let Ok(value) = x.field { plus_one(value + captured); }
+
+
+    if let Ok(ref value) = x.field { do_nothing(value + captured) }
+}
+
+fn main() {}
diff --git a/tests/ui/result_map_unit_fn.rs b/tests/ui/result_map_unit_fn.rs
index a8e891d8db02..a63538133491 100644
--- a/tests/ui/result_map_unit_fn.rs
+++ b/tests/ui/result_map_unit_fn.rs
@@ -1,3 +1,6 @@
+// run-rustfix
+// rustfix-only-machine-applicable
+
 #![feature(never_type)]
 #![warn(clippy::result_map_unit_fn)]
 #![allow(unused)]
@@ -28,15 +31,6 @@ impl HasResult {
 fn result_map_unit_fn() {
     let x = HasResult { field: Ok(10) };
 
-    x.field.map(plus_one);
-    let _: Result<(), usize> = x.field.map(do_nothing);
-
-    x.field.map(do_nothing);
-
-    x.field.map(do_nothing);
-
-    x.field.map(diverge);
-
     let captured = 10;
     if let Ok(value) = x.field { do_nothing(value + captured) };
     let _: Result<(), usize> = x.field.map(|value| do_nothing(value + captured));
@@ -74,29 +68,6 @@ fn result_map_unit_fn() {
 
 
     x.field.map(|ref value| { do_nothing(value + captured) });
-
-
-    x.field.map(|value| { do_nothing(value); do_nothing(value) });
-
-    x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) });
-
-    // Suggestion for the let block should be `{ ... }` as it's too difficult to build a
-    // proper suggestion for these cases
-    x.field.map(|value| {
-        do_nothing(value);
-        do_nothing(value)
-    });
-    x.field.map(|value| { do_nothing(value); do_nothing(value); });
-
-    // The following should suggest `if let Ok(_X) ...` as it's difficult to generate a proper let variable name for them
-    let res: Result<!, usize> = Ok(42).map(diverge);
-    "12".parse::<i32>().map(diverge);
-
-    let res: Result<(), usize> = Ok(plus_one(1)).map(do_nothing);
-
-    // Should suggest `if let Ok(_y) ...` to not override the existing foo variable
-    let y: Result<usize, usize> = Ok(42);
-    y.map(do_nothing);
 }
 
 fn main() {}
diff --git a/tests/ui/result_map_unit_fn.stderr b/tests/ui/result_map_unit_fn.stderr
index 9f9025152e24..3181c4fef277 100644
--- a/tests/ui/result_map_unit_fn.stderr
+++ b/tests/ui/result_map_unit_fn.stderr
@@ -1,39 +1,15 @@
-error: called `map(f)` on an Result value where `f` is a unit function
-  --> $DIR/result_map_unit_fn.rs:34:5
-   |
-LL |     x.field.map(do_nothing);
-   |     ^^^^^^^^^^^^^^^^^^^^^^^-
-   |     |
-   |     help: try this: `if let Ok(x_field) = x.field { do_nothing(...) }`
-   |
-   = note: `-D clippy::result-map-unit-fn` implied by `-D warnings`
-
-error: called `map(f)` on an Result value where `f` is a unit function
-  --> $DIR/result_map_unit_fn.rs:36:5
-   |
-LL |     x.field.map(do_nothing);
-   |     ^^^^^^^^^^^^^^^^^^^^^^^-
-   |     |
-   |     help: try this: `if let Ok(x_field) = x.field { do_nothing(...) }`
-
-error: called `map(f)` on an Result value where `f` is a unit function
-  --> $DIR/result_map_unit_fn.rs:38:5
-   |
-LL |     x.field.map(diverge);
-   |     ^^^^^^^^^^^^^^^^^^^^-
-   |     |
-   |     help: try this: `if let Ok(x_field) = x.field { diverge(...) }`
-
 error: called `map(f)` on an Result value where `f` is a unit closure
-  --> $DIR/result_map_unit_fn.rs:44:5
+  --> $DIR/result_map_unit_fn.rs:38:5
    |
 LL |     x.field.map(|value| x.do_result_nothing(value + captured));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
    |     |
    |     help: try this: `if let Ok(value) = x.field { x.do_result_nothing(value + captured) }`
+   |
+   = note: `-D clippy::result-map-unit-fn` implied by `-D warnings`
 
 error: called `map(f)` on an Result value where `f` is a unit closure
-  --> $DIR/result_map_unit_fn.rs:46:5
+  --> $DIR/result_map_unit_fn.rs:40:5
    |
 LL |     x.field.map(|value| { x.do_result_plus_one(value + captured); });
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
@@ -41,7 +17,7 @@ LL |     x.field.map(|value| { x.do_result_plus_one(value + captured); });
    |     help: try this: `if let Ok(value) = x.field { x.do_result_plus_one(value + captured); }`
 
 error: called `map(f)` on an Result value where `f` is a unit closure
-  --> $DIR/result_map_unit_fn.rs:49:5
+  --> $DIR/result_map_unit_fn.rs:43:5
    |
 LL |     x.field.map(|value| do_nothing(value + captured));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
@@ -49,7 +25,7 @@ LL |     x.field.map(|value| do_nothing(value + captured));
    |     help: try this: `if let Ok(value) = x.field { do_nothing(value + captured) }`
 
 error: called `map(f)` on an Result value where `f` is a unit closure
-  --> $DIR/result_map_unit_fn.rs:51:5
+  --> $DIR/result_map_unit_fn.rs:45:5
    |
 LL |     x.field.map(|value| { do_nothing(value + captured) });
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
@@ -57,7 +33,7 @@ LL |     x.field.map(|value| { do_nothing(value + captured) });
    |     help: try this: `if let Ok(value) = x.field { do_nothing(value + captured) }`
 
 error: called `map(f)` on an Result value where `f` is a unit closure
-  --> $DIR/result_map_unit_fn.rs:53:5
+  --> $DIR/result_map_unit_fn.rs:47:5
    |
 LL |     x.field.map(|value| { do_nothing(value + captured); });
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
@@ -65,7 +41,7 @@ LL |     x.field.map(|value| { do_nothing(value + captured); });
    |     help: try this: `if let Ok(value) = x.field { do_nothing(value + captured); }`
 
 error: called `map(f)` on an Result value where `f` is a unit closure
-  --> $DIR/result_map_unit_fn.rs:55:5
+  --> $DIR/result_map_unit_fn.rs:49:5
    |
 LL |     x.field.map(|value| { { do_nothing(value + captured); } });
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
@@ -73,7 +49,7 @@ LL |     x.field.map(|value| { { do_nothing(value + captured); } });
    |     help: try this: `if let Ok(value) = x.field { do_nothing(value + captured); }`
 
 error: called `map(f)` on an Result value where `f` is a unit closure
-  --> $DIR/result_map_unit_fn.rs:58:5
+  --> $DIR/result_map_unit_fn.rs:52:5
    |
 LL |     x.field.map(|value| diverge(value + captured));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
@@ -81,7 +57,7 @@ LL |     x.field.map(|value| diverge(value + captured));
    |     help: try this: `if let Ok(value) = x.field { diverge(value + captured) }`
 
 error: called `map(f)` on an Result value where `f` is a unit closure
-  --> $DIR/result_map_unit_fn.rs:60:5
+  --> $DIR/result_map_unit_fn.rs:54:5
    |
 LL |     x.field.map(|value| { diverge(value + captured) });
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
@@ -89,7 +65,7 @@ LL |     x.field.map(|value| { diverge(value + captured) });
    |     help: try this: `if let Ok(value) = x.field { diverge(value + captured) }`
 
 error: called `map(f)` on an Result value where `f` is a unit closure
-  --> $DIR/result_map_unit_fn.rs:62:5
+  --> $DIR/result_map_unit_fn.rs:56:5
    |
 LL |     x.field.map(|value| { diverge(value + captured); });
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
@@ -97,7 +73,7 @@ LL |     x.field.map(|value| { diverge(value + captured); });
    |     help: try this: `if let Ok(value) = x.field { diverge(value + captured); }`
 
 error: called `map(f)` on an Result value where `f` is a unit closure
-  --> $DIR/result_map_unit_fn.rs:64:5
+  --> $DIR/result_map_unit_fn.rs:58:5
    |
 LL |     x.field.map(|value| { { diverge(value + captured); } });
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
@@ -105,7 +81,7 @@ LL |     x.field.map(|value| { { diverge(value + captured); } });
    |     help: try this: `if let Ok(value) = x.field { diverge(value + captured); }`
 
 error: called `map(f)` on an Result value where `f` is a unit closure
-  --> $DIR/result_map_unit_fn.rs:69:5
+  --> $DIR/result_map_unit_fn.rs:63:5
    |
 LL |     x.field.map(|value| { let y = plus_one(value + captured); });
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
@@ -113,7 +89,7 @@ LL |     x.field.map(|value| { let y = plus_one(value + captured); });
    |     help: try this: `if let Ok(value) = x.field { let y = plus_one(value + captured); }`
 
 error: called `map(f)` on an Result value where `f` is a unit closure
-  --> $DIR/result_map_unit_fn.rs:71:5
+  --> $DIR/result_map_unit_fn.rs:65:5
    |
 LL |     x.field.map(|value| { plus_one(value + captured); });
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
@@ -121,7 +97,7 @@ LL |     x.field.map(|value| { plus_one(value + captured); });
    |     help: try this: `if let Ok(value) = x.field { plus_one(value + captured); }`
 
 error: called `map(f)` on an Result value where `f` is a unit closure
-  --> $DIR/result_map_unit_fn.rs:73:5
+  --> $DIR/result_map_unit_fn.rs:67:5
    |
 LL |     x.field.map(|value| { { plus_one(value + captured); } });
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
@@ -129,66 +105,12 @@ LL |     x.field.map(|value| { { plus_one(value + captured); } });
    |     help: try this: `if let Ok(value) = x.field { plus_one(value + captured); }`
 
 error: called `map(f)` on an Result value where `f` is a unit closure
-  --> $DIR/result_map_unit_fn.rs:76:5
+  --> $DIR/result_map_unit_fn.rs:70:5
    |
 LL |     x.field.map(|ref value| { do_nothing(value + captured) });
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
    |     |
    |     help: try this: `if let Ok(ref value) = x.field { do_nothing(value + captured) }`
 
-error: called `map(f)` on an Result value where `f` is a unit closure
-  --> $DIR/result_map_unit_fn.rs:79:5
-   |
-LL |     x.field.map(|value| { do_nothing(value); do_nothing(value) });
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
-   |     |
-   |     help: try this: `if let Ok(value) = x.field { ... }`
-
-error: called `map(f)` on an Result value where `f` is a unit closure
-  --> $DIR/result_map_unit_fn.rs:81:5
-   |
-LL |     x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) });
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
-   |     |
-   |     help: try this: `if let Ok(value) = x.field { ... }`
-
-error: called `map(f)` on an Result value where `f` is a unit closure
-  --> $DIR/result_map_unit_fn.rs:85:5
-   |
-LL |        x.field.map(|value| {
-   |   _____^
-   |  |_____|
-   | ||
-LL | ||         do_nothing(value);
-LL | ||         do_nothing(value)
-LL | ||     });
-   | ||______^- help: try this: `if let Ok(value) = x.field { ... }`
-   | |_______|
-   | 
-
-error: called `map(f)` on an Result value where `f` is a unit closure
-  --> $DIR/result_map_unit_fn.rs:89:5
-   |
-LL |     x.field.map(|value| { do_nothing(value); do_nothing(value); });
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
-   |     |
-   |     help: try this: `if let Ok(value) = x.field { ... }`
-
-error: called `map(f)` on an Result value where `f` is a unit function
-  --> $DIR/result_map_unit_fn.rs:93:5
-   |
-LL |     "12".parse::<i32>().map(diverge);
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
-   |     |
-   |     help: try this: `if let Ok(_) = "12".parse::<i32>() { diverge(...) }`
-
-error: called `map(f)` on an Result value where `f` is a unit function
-  --> $DIR/result_map_unit_fn.rs:99:5
-   |
-LL |     y.map(do_nothing);
-   |     ^^^^^^^^^^^^^^^^^-
-   |     |
-   |     help: try this: `if let Ok(_y) = y { do_nothing(...) }`
-
-error: aborting due to 23 previous errors
+error: aborting due to 14 previous errors
 
diff --git a/tests/ui/result_map_unit_fn_non_rustfix.rs b/tests/ui/result_map_unit_fn_non_rustfix.rs
new file mode 100644
index 000000000000..6bd5d579a13e
--- /dev/null
+++ b/tests/ui/result_map_unit_fn_non_rustfix.rs
@@ -0,0 +1,74 @@
+#![feature(never_type)]
+#![warn(clippy::result_map_unit_fn)]
+#![allow(unused)]
+
+fn do_nothing<T>(_: T) {}
+
+fn diverge<T>(_: T) -> ! {
+    panic!()
+}
+
+fn plus_one(value: usize) -> usize {
+    value + 1
+}
+
+struct HasResult {
+    field: Result<usize, usize>,
+}
+
+impl HasResult {
+    fn do_result_nothing(self: &Self, value: usize) {}
+
+    fn do_result_plus_one(self: &Self, value: usize) -> usize {
+        value + 1
+    }
+}
+
+fn result_map_unit_fn() {
+    let x = HasResult { field: Ok(10) };
+
+    x.field.map(plus_one);
+    let _: Result<(), usize> = x.field.map(do_nothing);
+
+    x.field.map(do_nothing);
+
+    x.field.map(do_nothing);
+
+    x.field.map(diverge);
+
+    x.field.map(|value| {
+        do_nothing(value);
+        do_nothing(value)
+    });
+
+    x.field.map(|value| {
+        if value > 0 {
+            do_nothing(value);
+            do_nothing(value)
+        }
+    });
+
+    // Suggestion for the let block should be `{ ... }` as it's too difficult to build a
+    // proper suggestion for these cases
+    x.field.map(|value| {
+        do_nothing(value);
+        do_nothing(value)
+    });
+    x.field.map(|value| {
+        do_nothing(value);
+        do_nothing(value);
+    });
+
+    // The following should suggest `if let Ok(_X) ...` as it's difficult to generate a proper let
+    // variable name for them
+    let res: Result<!, usize> = Ok(42).map(diverge);
+    "12".parse::<i32>().map(diverge);
+
+    let res: Result<(), usize> = Ok(plus_one(1)).map(do_nothing);
+
+    // Should suggest `if let Ok(_y) ...` to not override the existing foo variable
+    let y: Result<usize, usize> = Ok(42);
+    y.map(do_nothing);
+}
+
+fn main() {}
diff --git a/tests/ui/result_map_unit_fn_non_rustfix.stderr b/tests/ui/result_map_unit_fn_non_rustfix.stderr
new file mode 100644
index 000000000000..34a1e0d87a6c
--- /dev/null
+++ b/tests/ui/result_map_unit_fn_non_rustfix.stderr
@@ -0,0 +1,102 @@
+error: called `map(f)` on an Result value where `f` is a unit function
+  --> $DIR/result_map_unit_fn_non_rustfix.rs:33:5
+   |
+LL |     x.field.map(do_nothing);
+   |     ^^^^^^^^^^^^^^^^^^^^^^^-
+   |     |
+   |     help: try this: `if let Ok(x_field) = x.field { do_nothing(...) }`
+   |
+   = note: `-D clippy::result-map-unit-fn` implied by `-D warnings`
+
+error: called `map(f)` on an Result value where `f` is a unit function
+  --> $DIR/result_map_unit_fn_non_rustfix.rs:35:5
+   |
+LL |     x.field.map(do_nothing);
+   |     ^^^^^^^^^^^^^^^^^^^^^^^-
+   |     |
+   |     help: try this: `if let Ok(x_field) = x.field { do_nothing(...) }`
+
+error: called `map(f)` on an Result value where `f` is a unit function
+  --> $DIR/result_map_unit_fn_non_rustfix.rs:37:5
+   |
+LL |     x.field.map(diverge);
+   |     ^^^^^^^^^^^^^^^^^^^^-
+   |     |
+   |     help: try this: `if let Ok(x_field) = x.field { diverge(...) }`
+
+error: called `map(f)` on an Result value where `f` is a unit closure
+  --> $DIR/result_map_unit_fn_non_rustfix.rs:39:5
+   |
+LL |        x.field.map(|value| {
+   |   _____^
+   |  |_____|
+   | ||
+LL | ||         do_nothing(value);
+LL | ||         do_nothing(value)
+LL | ||     });
+   | ||______^- help: try this: `if let Ok(value) = x.field { ... }`
+   | |_______|
+   | 
+
+error: called `map(f)` on an Result value where `f` is a unit closure
+  --> $DIR/result_map_unit_fn_non_rustfix.rs:44:5
+   |
+LL |        x.field.map(|value| {
+   |   _____^
+   |  |_____|
+   | ||
+LL | ||         if value > 0 {
+LL | ||             do_nothing(value);
+LL | ||             do_nothing(value)
+LL | ||         }
+LL | ||     });
+   | ||______^- help: try this: `if let Ok(value) = x.field { ... }`
+   | |_______|
+   | 
+
+error: called `map(f)` on an Result value where `f` is a unit closure
+  --> $DIR/result_map_unit_fn_non_rustfix.rs:53:5
+   |
+LL |        x.field.map(|value| {
+   |   _____^
+   |  |_____|
+   | ||
+LL | ||         do_nothing(value);
+LL | ||         do_nothing(value)
+LL | ||     });
+   | ||______^- help: try this: `if let Ok(value) = x.field { ... }`
+   | |_______|
+   | 
+
+error: called `map(f)` on an Result value where `f` is a unit closure
+  --> $DIR/result_map_unit_fn_non_rustfix.rs:57:5
+   |
+LL |        x.field.map(|value| {
+   |   _____^
+   |  |_____|
+   | ||
+LL | ||         do_nothing(value);
+LL | ||         do_nothing(value);
+LL | ||     });
+   | ||______^- help: try this: `if let Ok(value) = x.field { ... }`
+   | |_______|
+   | 
+
+error: called `map(f)` on an Result value where `f` is a unit function
+  --> $DIR/result_map_unit_fn_non_rustfix.rs:65:5
+   |
+LL |     "12".parse::<i32>().map(diverge);
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
+   |     |
+   |     help: try this: `if let Ok(_) = "12".parse::<i32>() { diverge(...) }`
+
+error: called `map(f)` on an Result value where `f` is a unit function
+  --> $DIR/result_map_unit_fn_non_rustfix.rs:71:5
+   |
+LL |     y.map(do_nothing);
+   |     ^^^^^^^^^^^^^^^^^-
+   |     |
+   |     help: try this: `if let Ok(_y) = y { do_nothing(...) }`
+
+error: aborting due to 9 previous errors
+