diff options
| author | Joe Frikker <jfrikker@gmail.com> | 2019-06-24 21:28:46 -0400 |
|---|---|---|
| committer | Joe Frikker <jfrikker@gmail.com> | 2019-06-24 21:28:46 -0400 |
| commit | 8880677b4f7337861d98afb7742968f794dbf1ef (patch) | |
| tree | 5f4e504450fc9b8cd10e9296cd5f403db7ca27ac | |
| parent | 1e6c6976dd12406d2b57de17f1f667527d7977c6 (diff) | |
| download | rust-8880677b4f7337861d98afb7742968f794dbf1ef.tar.gz rust-8880677b4f7337861d98afb7742968f794dbf1ef.zip | |
Making try_err machine applicable
| -rw-r--r-- | clippy_lints/src/try_err.rs | 2 | ||||
| -rw-r--r-- | tests/ui/try_err.fixed | 75 | ||||
| -rw-r--r-- | tests/ui/try_err.rs | 18 | ||||
| -rw-r--r-- | tests/ui/try_err.stderr | 26 |
4 files changed, 103 insertions, 18 deletions
diff --git a/clippy_lints/src/try_err.rs b/clippy_lints/src/try_err.rs index eec13545956..7466221fb11 100644 --- a/clippy_lints/src/try_err.rs +++ b/clippy_lints/src/try_err.rs @@ -80,7 +80,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr { "returning an `Err(_)` with the `?` operator", "try this", suggestion, - Applicability::MaybeIncorrect + Applicability::MachineApplicable ); } } diff --git a/tests/ui/try_err.fixed b/tests/ui/try_err.fixed new file mode 100644 index 00000000000..fe4be727be1 --- /dev/null +++ b/tests/ui/try_err.fixed @@ -0,0 +1,75 @@ +// run-rustfix + +#![deny(clippy::try_err)] + +// Tests that a simple case works +// Should flag `Err(err)?` +pub fn basic_test() -> Result<i32, i32> { + let err: i32 = 1; + if true { // To avoid warnings during rustfix + return Err(err); + } + Ok(0) +} + +// Tests that `.into()` is added when appropriate +pub fn into_test() -> Result<i32, i32> { + let err: u8 = 1; + if true { // To avoid warnings during rustfix + return Err(err.into()); + } + Ok(0) +} + +// Tests that tries in general don't trigger the error +pub fn negative_test() -> Result<i32, i32> { + Ok(nested_error()? + 1) +} + + +// Tests that `.into()` isn't added when the error type +// matches the surrounding closure's return type, even +// when it doesn't match the surrounding function's. +pub fn closure_matches_test() -> Result<i32, i32> { + let res: Result<i32, i8> = Some(1).into_iter() + .map(|i| { + let err: i8 = 1; + if true { // To avoid warnings during rustfix + return Err(err); + } + Ok(i) + }) + .next() + .unwrap(); + + Ok(res?) +} + +// Tests that `.into()` isn't added when the error type +// doesn't match the surrounding closure's return type. +pub fn closure_into_test() -> Result<i32, i32> { + let res: Result<i32, i16> = Some(1).into_iter() + .map(|i| { + let err: i8 = 1; + if true { // To avoid warnings during rustfix + return Err(err.into()); + } + Ok(i) + }) + .next() + .unwrap(); + + Ok(res?) +} + +fn nested_error() -> Result<i32, i32> { + Ok(1) +} + +fn main() { + basic_test().unwrap(); + into_test().unwrap(); + negative_test().unwrap(); + closure_matches_test().unwrap(); + closure_into_test().unwrap(); +} diff --git a/tests/ui/try_err.rs b/tests/ui/try_err.rs index d8decbbac66..c72f88da8e3 100644 --- a/tests/ui/try_err.rs +++ b/tests/ui/try_err.rs @@ -1,17 +1,23 @@ +// run-rustfix + #![deny(clippy::try_err)] // Tests that a simple case works // Should flag `Err(err)?` pub fn basic_test() -> Result<i32, i32> { let err: i32 = 1; - Err(err)?; + if true { // To avoid warnings during rustfix + Err(err)?; + } Ok(0) } // Tests that `.into()` is added when appropriate pub fn into_test() -> Result<i32, i32> { let err: u8 = 1; - Err(err)?; + if true { // To avoid warnings during rustfix + Err(err)?; + } Ok(0) } @@ -28,7 +34,9 @@ pub fn closure_matches_test() -> Result<i32, i32> { let res: Result<i32, i8> = Some(1).into_iter() .map(|i| { let err: i8 = 1; - Err(err)?; + if true { // To avoid warnings during rustfix + Err(err)?; + } Ok(i) }) .next() @@ -43,7 +51,9 @@ pub fn closure_into_test() -> Result<i32, i32> { let res: Result<i32, i16> = Some(1).into_iter() .map(|i| { let err: i8 = 1; - Err(err)?; + if true { // To avoid warnings during rustfix + Err(err)?; + } Ok(i) }) .next() diff --git a/tests/ui/try_err.stderr b/tests/ui/try_err.stderr index b2fb35ffb51..2d095f1b6e0 100644 --- a/tests/ui/try_err.stderr +++ b/tests/ui/try_err.stderr @@ -1,32 +1,32 @@ error: returning an `Err(_)` with the `?` operator - --> $DIR/try_err.rs:7:5 + --> $DIR/try_err.rs:10:9 | -LL | Err(err)?; - | ^^^^^^^^^ help: try this: `return Err(err)` +LL | Err(err)?; + | ^^^^^^^^^ help: try this: `return Err(err)` | note: lint level defined here - --> $DIR/try_err.rs:1:9 + --> $DIR/try_err.rs:3:9 | LL | #![deny(clippy::try_err)] | ^^^^^^^^^^^^^^^ error: returning an `Err(_)` with the `?` operator - --> $DIR/try_err.rs:14:5 + --> $DIR/try_err.rs:19:9 | -LL | Err(err)?; - | ^^^^^^^^^ help: try this: `return Err(err.into())` +LL | Err(err)?; + | ^^^^^^^^^ help: try this: `return Err(err.into())` error: returning an `Err(_)` with the `?` operator - --> $DIR/try_err.rs:31:13 + --> $DIR/try_err.rs:38:17 | -LL | Err(err)?; - | ^^^^^^^^^ help: try this: `return Err(err)` +LL | Err(err)?; + | ^^^^^^^^^ help: try this: `return Err(err)` error: returning an `Err(_)` with the `?` operator - --> $DIR/try_err.rs:46:13 + --> $DIR/try_err.rs:55:17 | -LL | Err(err)?; - | ^^^^^^^^^ help: try this: `return Err(err.into())` +LL | Err(err)?; + | ^^^^^^^^^ help: try this: `return Err(err.into())` error: aborting due to 4 previous errors |
