about summary refs log tree commit diff
diff options
context:
space:
mode:
authorThibsG <Thibs@debian.com>2020-10-23 09:24:25 +0200
committerThibsG <Thibs@debian.com>2020-10-26 11:02:07 +0100
commit2a3ae114858ff971b4cc51b4a43cb1475bd39516 (patch)
treedce3984f69c328a175893b7a77c3a2902ab8a801
parent3fec6f568daababf3520e2a818b4c1db79266b92 (diff)
downloadrust-2a3ae114858ff971b4cc51b4a43cb1475bd39516.tar.gz
rust-2a3ae114858ff971b4cc51b4a43cb1475bd39516.zip
Move fixable `map_unwrap_or` cases to rustfixed test
-rw-r--r--tests/ui/map_unwrap_or.rs41
-rw-r--r--tests/ui/map_unwrap_or.stderr61
-rw-r--r--tests/ui/map_unwrap_or_else_fixable.fixed59
-rw-r--r--tests/ui/map_unwrap_or_else_fixable.rs63
-rw-r--r--tests/ui/map_unwrap_or_else_fixable.stderr40
5 files changed, 172 insertions, 92 deletions
diff --git a/tests/ui/map_unwrap_or.rs b/tests/ui/map_unwrap_or.rs
index 585944032e7..4e977051ab7 100644
--- a/tests/ui/map_unwrap_or.rs
+++ b/tests/ui/map_unwrap_or.rs
@@ -1,4 +1,3 @@
-// FIXME: Add "run-rustfix" once it's supported for multipart suggestions
 // aux-build:option_helpers.rs
 
 #![warn(clippy::map_unwrap_or)]
@@ -13,10 +12,6 @@ fn option_methods() {
     let opt = Some(1);
 
     // Check for `option.map(_).unwrap_or(_)` use.
-    // Single line case.
-    let _ = opt.map(|x| x + 1)
-        // Should lint even though this call is on a separate line.
-        .unwrap_or(0);
     // Multi-line cases.
     let _ = opt.map(|x| {
         x + 1
@@ -47,10 +42,6 @@ fn option_methods() {
     let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
 
     // Check for `option.map(_).unwrap_or_else(_)` use.
-    // single line case
-    let _ = opt.map(|x| x + 1)
-        // Should lint even though this call is on a separate line.
-        .unwrap_or_else(|| 0);
     // Multi-line cases.
     let _ = opt.map(|x| {
         x + 1
@@ -60,40 +51,8 @@ fn option_methods() {
         .unwrap_or_else(||
             0
         );
-    // Macro case.
-    // Should not lint.
-    let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
-
-    // Issue #4144
-    {
-        let mut frequencies = HashMap::new();
-        let word = "foo";
-
-        frequencies
-            .get_mut(word)
-            .map(|count| {
-                *count += 1;
-            })
-            .unwrap_or_else(|| {
-                frequencies.insert(word.to_owned(), 1);
-            });
-    }
-}
-
-fn result_methods() {
-    let res: Result<i32, ()> = Ok(1);
-
-    // Check for `result.map(_).unwrap_or_else(_)` use.
-    // single line case
-    let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0); // should lint even though this call is on a separate line
-                                                      // multi line cases
-    let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
-    let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
-    // macro case
-    let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|e| 0); // should not lint
 }
 
 fn main() {
     option_methods();
-    result_methods();
 }
diff --git a/tests/ui/map_unwrap_or.stderr b/tests/ui/map_unwrap_or.stderr
index 1975abb3e9f..3fd4bdfd2b9 100644
--- a/tests/ui/map_unwrap_or.stderr
+++ b/tests/ui/map_unwrap_or.stderr
@@ -1,20 +1,5 @@
 error: called `map(<f>).unwrap_or(<a>)` on an `Option` value. This can be done more directly by calling `map_or(<a>, <f>)` instead
-  --> $DIR/map_unwrap_or.rs:17:13
-   |
-LL |       let _ = opt.map(|x| x + 1)
-   |  _____________^
-LL | |         // Should lint even though this call is on a separate line.
-LL | |         .unwrap_or(0);
-   | |_____________________^
-   |
-   = note: `-D clippy::map-unwrap-or` implied by `-D warnings`
-help: use `map_or(<a>, <f>)` instead
-   |
-LL |     let _ = opt.map_or(0, |x| x + 1);
-   |                 ^^^^^^ ^^          --
-
-error: called `map(<f>).unwrap_or(<a>)` on an `Option` value. This can be done more directly by calling `map_or(<a>, <f>)` instead
-  --> $DIR/map_unwrap_or.rs:21:13
+  --> $DIR/map_unwrap_or.rs:16:13
    |
 LL |       let _ = opt.map(|x| {
    |  _____________^
@@ -23,6 +8,7 @@ LL | |     }
 LL | |     ).unwrap_or(0);
    | |__________________^
    |
+   = note: `-D clippy::map-unwrap-or` implied by `-D warnings`
 help: use `map_or(<a>, <f>)` instead
    |
 LL |     let _ = opt.map_or(0, |x| {
@@ -32,7 +18,7 @@ LL |     );
    |
 
 error: called `map(<f>).unwrap_or(<a>)` on an `Option` value. This can be done more directly by calling `map_or(<a>, <f>)` instead
-  --> $DIR/map_unwrap_or.rs:25:13
+  --> $DIR/map_unwrap_or.rs:20:13
    |
 LL |       let _ = opt.map(|x| x + 1)
    |  _____________^
@@ -49,7 +35,7 @@ LL |         }, |x| x + 1);
    |
 
 error: called `map(<f>).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(<f>)` instead
-  --> $DIR/map_unwrap_or.rs:30:13
+  --> $DIR/map_unwrap_or.rs:25:13
    |
 LL |     let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -60,7 +46,7 @@ LL |     let _ = opt.and_then(|x| Some(x + 1));
    |                 ^^^^^^^^                --
 
 error: called `map(<f>).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(<f>)` instead
-  --> $DIR/map_unwrap_or.rs:32:13
+  --> $DIR/map_unwrap_or.rs:27:13
    |
 LL |       let _ = opt.map(|x| {
    |  _____________^
@@ -78,7 +64,7 @@ LL |     );
    |
 
 error: called `map(<f>).unwrap_or(None)` on an `Option` value. This can be done more directly by calling `and_then(<f>)` instead
-  --> $DIR/map_unwrap_or.rs:36:13
+  --> $DIR/map_unwrap_or.rs:31:13
    |
 LL |       let _ = opt
    |  _____________^
@@ -92,7 +78,7 @@ LL |         .and_then(|x| Some(x + 1));
    |          ^^^^^^^^                --
 
 error: called `map(<f>).unwrap_or(<a>)` on an `Option` value. This can be done more directly by calling `map_or(<a>, <f>)` instead
-  --> $DIR/map_unwrap_or.rs:47:13
+  --> $DIR/map_unwrap_or.rs:42:13
    |
 LL |     let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -103,16 +89,7 @@ LL |     let _ = Some("prefix").map_or(id, |p| format!("{}.", p));
    |                            ^^^^^^ ^^^                      --
 
 error: called `map(<f>).unwrap_or_else(<g>)` on an `Option` value. This can be done more directly by calling `map_or_else(<g>, <f>)` instead
-  --> $DIR/map_unwrap_or.rs:51:13
-   |
-LL |       let _ = opt.map(|x| x + 1)
-   |  _____________^
-LL | |         // Should lint even though this call is on a separate line.
-LL | |         .unwrap_or_else(|| 0);
-   | |_____________________________^ help: try this: `opt.map_or_else(|| 0, |x| x + 1)`
-
-error: called `map(<f>).unwrap_or_else(<g>)` on an `Option` value. This can be done more directly by calling `map_or_else(<g>, <f>)` instead
-  --> $DIR/map_unwrap_or.rs:55:13
+  --> $DIR/map_unwrap_or.rs:46:13
    |
 LL |       let _ = opt.map(|x| {
    |  _____________^
@@ -122,7 +99,7 @@ LL | |     ).unwrap_or_else(|| 0);
    | |__________________________^
 
 error: called `map(<f>).unwrap_or_else(<g>)` on an `Option` value. This can be done more directly by calling `map_or_else(<g>, <f>)` instead
-  --> $DIR/map_unwrap_or.rs:59:13
+  --> $DIR/map_unwrap_or.rs:50:13
    |
 LL |       let _ = opt.map(|x| x + 1)
    |  _____________^
@@ -131,23 +108,5 @@ LL | |             0
 LL | |         );
    | |_________^
 
-error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value. This can be done more directly by calling `.map_or_else(<g>, <f>)` instead
-  --> $DIR/map_unwrap_or.rs:88:13
-   |
-LL |     let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0); // should lint even though this call is on a separate line
-   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `res.map_or_else(|e| 0, |x| x + 1)`
-
-error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value. This can be done more directly by calling `.map_or_else(<g>, <f>)` instead
-  --> $DIR/map_unwrap_or.rs:90:13
-   |
-LL |     let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
-   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `res.map_or_else(|e| 0, |x| x + 1)`
-
-error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value. This can be done more directly by calling `.map_or_else(<g>, <f>)` instead
-  --> $DIR/map_unwrap_or.rs:91:13
-   |
-LL |     let _ = res.map(|x| x + 1).unwrap_or_else(|e| 0);
-   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `res.map_or_else(|e| 0, |x| x + 1)`
-
-error: aborting due to 13 previous errors
+error: aborting due to 8 previous errors
 
diff --git a/tests/ui/map_unwrap_or_else_fixable.fixed b/tests/ui/map_unwrap_or_else_fixable.fixed
new file mode 100644
index 00000000000..cb2492a3be0
--- /dev/null
+++ b/tests/ui/map_unwrap_or_else_fixable.fixed
@@ -0,0 +1,59 @@
+// run-rustfix
+// aux-build:option_helpers.rs
+
+#![warn(clippy::map_unwrap_or)]
+
+#[macro_use]
+extern crate option_helpers;
+
+use std::collections::HashMap;
+
+#[rustfmt::skip]
+fn option_methods() {
+    let opt = Some(1);
+
+    // Check for `option.map(_).unwrap_or_else(_)` use.
+    // single line case
+    let _ = opt.map_or_else(|| 0, |x| x + 1);
+
+    // Macro case.
+    // Should not lint.
+    let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
+
+    // Check for `option.map(_).unwrap_or_else(_)` use.
+    // single line case
+    let _ = opt.map_or_else(|| 0, |x| x + 1);
+
+    // Issue #4144
+    {
+        let mut frequencies = HashMap::new();
+        let word = "foo";
+
+        frequencies
+            .get_mut(word)
+            .map(|count| {
+                *count += 1;
+            })
+            .unwrap_or_else(|| {
+                frequencies.insert(word.to_owned(), 1);
+            });
+    }
+}
+
+fn result_methods() {
+    let res: Result<i32, ()> = Ok(1);
+
+    // Check for `result.map(_).unwrap_or_else(_)` use.
+    // single line case
+    let _ = res.map_or_else(|_e| 0, |x| x + 1); // should lint even though this call is on a separate line
+                                                       // multi line cases
+    let _ = res.map_or_else(|_e| 0, |x| x + 1);
+    let _ = res.map_or_else(|_e| 0, |x| x + 1);
+    // macro case
+    let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|_e| 0); // should not lint
+}
+
+fn main() {
+    option_methods();
+    result_methods();
+}
diff --git a/tests/ui/map_unwrap_or_else_fixable.rs b/tests/ui/map_unwrap_or_else_fixable.rs
new file mode 100644
index 00000000000..ed762dacd87
--- /dev/null
+++ b/tests/ui/map_unwrap_or_else_fixable.rs
@@ -0,0 +1,63 @@
+// run-rustfix
+// aux-build:option_helpers.rs
+
+#![warn(clippy::map_unwrap_or)]
+
+#[macro_use]
+extern crate option_helpers;
+
+use std::collections::HashMap;
+
+#[rustfmt::skip]
+fn option_methods() {
+    let opt = Some(1);
+
+    // Check for `option.map(_).unwrap_or_else(_)` use.
+    // single line case
+    let _ = opt.map(|x| x + 1)
+        // Should lint even though this call is on a separate line.
+        .unwrap_or_else(|| 0);
+
+    // Macro case.
+    // Should not lint.
+    let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0);
+
+    // Check for `option.map(_).unwrap_or_else(_)` use.
+    // single line case
+    let _ = opt.map(|x| x + 1)
+        // Should lint even though this call is on a separate line.
+        .unwrap_or_else(|| 0);
+
+    // Issue #4144
+    {
+        let mut frequencies = HashMap::new();
+        let word = "foo";
+
+        frequencies
+            .get_mut(word)
+            .map(|count| {
+                *count += 1;
+            })
+            .unwrap_or_else(|| {
+                frequencies.insert(word.to_owned(), 1);
+            });
+    }
+}
+
+fn result_methods() {
+    let res: Result<i32, ()> = Ok(1);
+
+    // Check for `result.map(_).unwrap_or_else(_)` use.
+    // single line case
+    let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0); // should lint even though this call is on a separate line
+                                                       // multi line cases
+    let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0);
+    let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0);
+    // macro case
+    let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|_e| 0); // should not lint
+}
+
+fn main() {
+    option_methods();
+    result_methods();
+}
diff --git a/tests/ui/map_unwrap_or_else_fixable.stderr b/tests/ui/map_unwrap_or_else_fixable.stderr
new file mode 100644
index 00000000000..2cb76d70684
--- /dev/null
+++ b/tests/ui/map_unwrap_or_else_fixable.stderr
@@ -0,0 +1,40 @@
+error: called `map(<f>).unwrap_or_else(<g>)` on an `Option` value. This can be done more directly by calling `map_or_else(<g>, <f>)` instead
+  --> $DIR/map_unwrap_or_else_fixable.rs:17:13
+   |
+LL |       let _ = opt.map(|x| x + 1)
+   |  _____________^
+LL | |         // Should lint even though this call is on a separate line.
+LL | |         .unwrap_or_else(|| 0);
+   | |_____________________________^ help: try this: `opt.map_or_else(|| 0, |x| x + 1)`
+   |
+   = note: `-D clippy::map-unwrap-or` implied by `-D warnings`
+
+error: called `map(<f>).unwrap_or_else(<g>)` on an `Option` value. This can be done more directly by calling `map_or_else(<g>, <f>)` instead
+  --> $DIR/map_unwrap_or_else_fixable.rs:27:13
+   |
+LL |       let _ = opt.map(|x| x + 1)
+   |  _____________^
+LL | |         // Should lint even though this call is on a separate line.
+LL | |         .unwrap_or_else(|| 0);
+   | |_____________________________^ help: try this: `opt.map_or_else(|| 0, |x| x + 1)`
+
+error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value. This can be done more directly by calling `.map_or_else(<g>, <f>)` instead
+  --> $DIR/map_unwrap_or_else_fixable.rs:52:13
+   |
+LL |     let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0); // should lint even though this call is on a separate line
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `res.map_or_else(|_e| 0, |x| x + 1)`
+
+error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value. This can be done more directly by calling `.map_or_else(<g>, <f>)` instead
+  --> $DIR/map_unwrap_or_else_fixable.rs:54:13
+   |
+LL |     let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0);
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `res.map_or_else(|_e| 0, |x| x + 1)`
+
+error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value. This can be done more directly by calling `.map_or_else(<g>, <f>)` instead
+  --> $DIR/map_unwrap_or_else_fixable.rs:55:13
+   |
+LL |     let _ = res.map(|x| x + 1).unwrap_or_else(|_e| 0);
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `res.map_or_else(|_e| 0, |x| x + 1)`
+
+error: aborting due to 5 previous errors
+