about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorTim Nielens <tim.nielens@gmail.com>2020-10-27 01:47:40 +0100
committerTim Nielens <tim.nielens@gmail.com>2020-10-27 01:57:04 +0100
commit111b9023dad65721300a39c3cf337f6bfb96d5d3 (patch)
tree59b64bd051668147b0d6ba4325dec5e648bc3e05 /tests
parentafbac8906e614a63ff5825710c3ebe45a3b5e01a (diff)
downloadrust-111b9023dad65721300a39c3cf337f6bfb96d5d3.tar.gz
rust-111b9023dad65721300a39c3cf337f6bfb96d5d3.zip
add manual_ok_or lint
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/manual_ok_or.fixed40
-rw-r--r--tests/ui/manual_ok_or.rs44
-rw-r--r--tests/ui/manual_ok_or.stderr41
3 files changed, 125 insertions, 0 deletions
diff --git a/tests/ui/manual_ok_or.fixed b/tests/ui/manual_ok_or.fixed
new file mode 100644
index 00000000000..b42e94bd727
--- /dev/null
+++ b/tests/ui/manual_ok_or.fixed
@@ -0,0 +1,40 @@
+// run-rustfix
+#![warn(clippy::manual_ok_or)]
+#![allow(clippy::blacklisted_name)]
+#![allow(clippy::redundant_closure)]
+#![allow(dead_code)]
+#![allow(unused_must_use)]
+
+fn main() {
+    // basic case
+    let foo: Option<i32> = None;
+    foo.ok_or("error");
+
+    // eta expansion case
+    foo.ok_or("error");
+
+    // turbo fish syntax
+    None::<i32>.ok_or("error");
+
+    // multiline case
+    #[rustfmt::skip]
+    foo.ok_or(&format!(
+        "{}{}{}{}{}{}{}",
+        "Alice", "Bob", "Sarah", "Marc", "Sandra", "Eric", "Jenifer"));
+
+    // not applicable, closure isn't direct `Ok` wrapping
+    foo.map_or(Err("error"), |v| Ok(v + 1));
+
+    // not applicable, or side isn't `Result::Err`
+    foo.map_or(Ok::<i32, &str>(1), |v| Ok(v));
+
+    // not applicatble, expr is not a `Result` value
+    foo.map_or(42, |v| v);
+
+    // TODO patterns not covered yet
+    match foo {
+        Some(v) => Ok(v),
+        None => Err("error"),
+    };
+    foo.map_or_else(|| Err("error"), |v| Ok(v));
+}
diff --git a/tests/ui/manual_ok_or.rs b/tests/ui/manual_ok_or.rs
new file mode 100644
index 00000000000..e5a6056fbf5
--- /dev/null
+++ b/tests/ui/manual_ok_or.rs
@@ -0,0 +1,44 @@
+// run-rustfix
+#![warn(clippy::manual_ok_or)]
+#![allow(clippy::blacklisted_name)]
+#![allow(clippy::redundant_closure)]
+#![allow(dead_code)]
+#![allow(unused_must_use)]
+
+fn main() {
+    // basic case
+    let foo: Option<i32> = None;
+    foo.map_or(Err("error"), |v| Ok(v));
+
+    // eta expansion case
+    foo.map_or(Err("error"), Ok);
+
+    // turbo fish syntax
+    None::<i32>.map_or(Err("error"), |v| Ok(v));
+
+    // multiline case
+    #[rustfmt::skip]
+    foo.map_or(Err::<i32, &str>(
+        &format!(
+            "{}{}{}{}{}{}{}",
+            "Alice", "Bob", "Sarah", "Marc", "Sandra", "Eric", "Jenifer")
+        ),
+        |v| Ok(v),
+    );
+
+    // not applicable, closure isn't direct `Ok` wrapping
+    foo.map_or(Err("error"), |v| Ok(v + 1));
+
+    // not applicable, or side isn't `Result::Err`
+    foo.map_or(Ok::<i32, &str>(1), |v| Ok(v));
+
+    // not applicatble, expr is not a `Result` value
+    foo.map_or(42, |v| v);
+
+    // TODO patterns not covered yet
+    match foo {
+        Some(v) => Ok(v),
+        None => Err("error"),
+    };
+    foo.map_or_else(|| Err("error"), |v| Ok(v));
+}
diff --git a/tests/ui/manual_ok_or.stderr b/tests/ui/manual_ok_or.stderr
new file mode 100644
index 00000000000..8ea10ac5436
--- /dev/null
+++ b/tests/ui/manual_ok_or.stderr
@@ -0,0 +1,41 @@
+error: this pattern reimplements `Option::ok_or`
+  --> $DIR/manual_ok_or.rs:11:5
+   |
+LL |     foo.map_or(Err("error"), |v| Ok(v));
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `foo.ok_or("error")`
+   |
+   = note: `-D clippy::manual-ok-or` implied by `-D warnings`
+
+error: this pattern reimplements `Option::ok_or`
+  --> $DIR/manual_ok_or.rs:14:5
+   |
+LL |     foo.map_or(Err("error"), Ok);
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `foo.ok_or("error")`
+
+error: this pattern reimplements `Option::ok_or`
+  --> $DIR/manual_ok_or.rs:17:5
+   |
+LL |     None::<i32>.map_or(Err("error"), |v| Ok(v));
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `None::<i32>.ok_or("error")`
+
+error: this pattern reimplements `Option::ok_or`
+  --> $DIR/manual_ok_or.rs:21:5
+   |
+LL | /     foo.map_or(Err::<i32, &str>(
+LL | |         &format!(
+LL | |             "{}{}{}{}{}{}{}",
+LL | |             "Alice", "Bob", "Sarah", "Marc", "Sandra", "Eric", "Jenifer")
+LL | |         ),
+LL | |         |v| Ok(v),
+LL | |     );
+   | |_____^
+   |
+help: replace with
+   |
+LL |     foo.ok_or(&format!(
+LL |         "{}{}{}{}{}{}{}",
+LL |         "Alice", "Bob", "Sarah", "Marc", "Sandra", "Eric", "Jenifer"));
+   |
+
+error: aborting due to 4 previous errors
+