about summary refs log tree commit diff
path: root/tests/ui
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-09-14 19:56:47 +0000
committerbors <bors@rust-lang.org>2020-09-14 19:56:47 +0000
commitf82e84c8943e70300ad1f4de46d8d7bd889c6bde (patch)
tree5b377fca586ae2fd583457e3a7ffb3c3cb382072 /tests/ui
parent231444d989db23ab53001e29af53fa844e0b08a1 (diff)
parentd719b485434eac557e65bf55cca79e63f7b83d5b (diff)
downloadrust-f82e84c8943e70300ad1f4de46d8d7bd889c6bde.tar.gz
rust-f82e84c8943e70300ad1f4de46d8d7bd889c6bde.zip
Auto merge of #5998 - deg4uss3r:master, r=yaahc
Add map_err_ignore lint

In a large code base a lot of times errors are ignored by using something like:

```rust
foo.map_err(|_| Some::Enum)?;
```

This drops the original error in favor of a enum that will not have the original error's context. This lint helps catch throwing away the original error in favor of an enum without its context.

---

*Please keep the line below*
changelog: Added map_err_ignore lint
Diffstat (limited to 'tests/ui')
-rw-r--r--tests/ui/drop_ref.rs1
-rw-r--r--tests/ui/drop_ref.stderr36
-rw-r--r--tests/ui/map_err.rs25
-rw-r--r--tests/ui/map_err.stderr11
4 files changed, 55 insertions, 18 deletions
diff --git a/tests/ui/drop_ref.rs b/tests/ui/drop_ref.rs
index 9181d789d4f..6b5bcdaa78e 100644
--- a/tests/ui/drop_ref.rs
+++ b/tests/ui/drop_ref.rs
@@ -1,5 +1,6 @@
 #![warn(clippy::drop_ref)]
 #![allow(clippy::toplevel_ref_arg)]
+#![allow(clippy::map_err_ignore)]
 
 use std::mem::drop;
 
diff --git a/tests/ui/drop_ref.stderr b/tests/ui/drop_ref.stderr
index 35ae88b78a4..7974bf56d44 100644
--- a/tests/ui/drop_ref.stderr
+++ b/tests/ui/drop_ref.stderr
@@ -1,108 +1,108 @@
 error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing.
-  --> $DIR/drop_ref.rs:9:5
+  --> $DIR/drop_ref.rs:10:5
    |
 LL |     drop(&SomeStruct);
    |     ^^^^^^^^^^^^^^^^^
    |
    = note: `-D clippy::drop-ref` implied by `-D warnings`
 note: argument has type `&SomeStruct`
-  --> $DIR/drop_ref.rs:9:10
+  --> $DIR/drop_ref.rs:10:10
    |
 LL |     drop(&SomeStruct);
    |          ^^^^^^^^^^^
 
 error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing.
-  --> $DIR/drop_ref.rs:12:5
+  --> $DIR/drop_ref.rs:13:5
    |
 LL |     drop(&owned1);
    |     ^^^^^^^^^^^^^
    |
 note: argument has type `&SomeStruct`
-  --> $DIR/drop_ref.rs:12:10
+  --> $DIR/drop_ref.rs:13:10
    |
 LL |     drop(&owned1);
    |          ^^^^^^^
 
 error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing.
-  --> $DIR/drop_ref.rs:13:5
+  --> $DIR/drop_ref.rs:14:5
    |
 LL |     drop(&&owned1);
    |     ^^^^^^^^^^^^^^
    |
 note: argument has type `&&SomeStruct`
-  --> $DIR/drop_ref.rs:13:10
+  --> $DIR/drop_ref.rs:14:10
    |
 LL |     drop(&&owned1);
    |          ^^^^^^^^
 
 error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing.
-  --> $DIR/drop_ref.rs:14:5
+  --> $DIR/drop_ref.rs:15:5
    |
 LL |     drop(&mut owned1);
    |     ^^^^^^^^^^^^^^^^^
    |
 note: argument has type `&mut SomeStruct`
-  --> $DIR/drop_ref.rs:14:10
+  --> $DIR/drop_ref.rs:15:10
    |
 LL |     drop(&mut owned1);
    |          ^^^^^^^^^^^
 
 error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing.
-  --> $DIR/drop_ref.rs:18:5
+  --> $DIR/drop_ref.rs:19:5
    |
 LL |     drop(reference1);
    |     ^^^^^^^^^^^^^^^^
    |
 note: argument has type `&SomeStruct`
-  --> $DIR/drop_ref.rs:18:10
+  --> $DIR/drop_ref.rs:19:10
    |
 LL |     drop(reference1);
    |          ^^^^^^^^^^
 
 error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing.
-  --> $DIR/drop_ref.rs:21:5
+  --> $DIR/drop_ref.rs:22:5
    |
 LL |     drop(reference2);
    |     ^^^^^^^^^^^^^^^^
    |
 note: argument has type `&mut SomeStruct`
-  --> $DIR/drop_ref.rs:21:10
+  --> $DIR/drop_ref.rs:22:10
    |
 LL |     drop(reference2);
    |          ^^^^^^^^^^
 
 error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing.
-  --> $DIR/drop_ref.rs:24:5
+  --> $DIR/drop_ref.rs:25:5
    |
 LL |     drop(reference3);
    |     ^^^^^^^^^^^^^^^^
    |
 note: argument has type `&SomeStruct`
-  --> $DIR/drop_ref.rs:24:10
+  --> $DIR/drop_ref.rs:25:10
    |
 LL |     drop(reference3);
    |          ^^^^^^^^^^
 
 error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing.
-  --> $DIR/drop_ref.rs:29:5
+  --> $DIR/drop_ref.rs:30:5
    |
 LL |     drop(&val);
    |     ^^^^^^^^^^
    |
 note: argument has type `&T`
-  --> $DIR/drop_ref.rs:29:10
+  --> $DIR/drop_ref.rs:30:10
    |
 LL |     drop(&val);
    |          ^^^^
 
 error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing.
-  --> $DIR/drop_ref.rs:37:5
+  --> $DIR/drop_ref.rs:38:5
    |
 LL |     std::mem::drop(&SomeStruct);
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
 note: argument has type `&SomeStruct`
-  --> $DIR/drop_ref.rs:37:20
+  --> $DIR/drop_ref.rs:38:20
    |
 LL |     std::mem::drop(&SomeStruct);
    |                    ^^^^^^^^^^^
diff --git a/tests/ui/map_err.rs b/tests/ui/map_err.rs
new file mode 100644
index 00000000000..617b6422872
--- /dev/null
+++ b/tests/ui/map_err.rs
@@ -0,0 +1,25 @@
+#![warn(clippy::map_err_ignore)]
+use std::convert::TryFrom;
+use std::error::Error;
+use std::fmt;
+
+#[derive(Debug)]
+enum Errors {
+    Ignored,
+}
+
+impl Error for Errors {}
+
+impl fmt::Display for Errors {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(f, "Error")
+    }
+}
+
+fn main() -> Result<(), Errors> {
+    let x = u32::try_from(-123_i32);
+
+    println!("{:?}", x.map_err(|_| Errors::Ignored));
+
+    Ok(())
+}
diff --git a/tests/ui/map_err.stderr b/tests/ui/map_err.stderr
new file mode 100644
index 00000000000..7273f460380
--- /dev/null
+++ b/tests/ui/map_err.stderr
@@ -0,0 +1,11 @@
+error: `map_err(|_|...` ignores the original error
+  --> $DIR/map_err.rs:22:32
+   |
+LL |     println!("{:?}", x.map_err(|_| Errors::Ignored));
+   |                                ^^^
+   |
+   = note: `-D clippy::map-err-ignore` implied by `-D warnings`
+   = help: Consider wrapping the error in an enum variant
+
+error: aborting due to previous error
+