mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 23:24:24 +00:00
Making try_err machine applicable
This commit is contained in:
parent
1e6c6976dd
commit
8880677b4f
4 changed files with 103 additions and 18 deletions
|
@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
75
tests/ui/try_err.fixed
Normal file
75
tests/ui/try_err.fixed
Normal file
|
@ -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();
|
||||
}
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue