Skip to content

Commit 8880677

Browse files
committed
Making try_err machine applicable
1 parent 1e6c697 commit 8880677

File tree

4 files changed

+103
-18
lines changed

4 files changed

+103
-18
lines changed

clippy_lints/src/try_err.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
8080
"returning an `Err(_)` with the `?` operator",
8181
"try this",
8282
suggestion,
83-
Applicability::MaybeIncorrect
83+
Applicability::MachineApplicable
8484
);
8585
}
8686
}

tests/ui/try_err.fixed

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// run-rustfix
2+
3+
#![deny(clippy::try_err)]
4+
5+
// Tests that a simple case works
6+
// Should flag `Err(err)?`
7+
pub fn basic_test() -> Result<i32, i32> {
8+
let err: i32 = 1;
9+
if true { // To avoid warnings during rustfix
10+
return Err(err);
11+
}
12+
Ok(0)
13+
}
14+
15+
// Tests that `.into()` is added when appropriate
16+
pub fn into_test() -> Result<i32, i32> {
17+
let err: u8 = 1;
18+
if true { // To avoid warnings during rustfix
19+
return Err(err.into());
20+
}
21+
Ok(0)
22+
}
23+
24+
// Tests that tries in general don't trigger the error
25+
pub fn negative_test() -> Result<i32, i32> {
26+
Ok(nested_error()? + 1)
27+
}
28+
29+
30+
// Tests that `.into()` isn't added when the error type
31+
// matches the surrounding closure's return type, even
32+
// when it doesn't match the surrounding function's.
33+
pub fn closure_matches_test() -> Result<i32, i32> {
34+
let res: Result<i32, i8> = Some(1).into_iter()
35+
.map(|i| {
36+
let err: i8 = 1;
37+
if true { // To avoid warnings during rustfix
38+
return Err(err);
39+
}
40+
Ok(i)
41+
})
42+
.next()
43+
.unwrap();
44+
45+
Ok(res?)
46+
}
47+
48+
// Tests that `.into()` isn't added when the error type
49+
// doesn't match the surrounding closure's return type.
50+
pub fn closure_into_test() -> Result<i32, i32> {
51+
let res: Result<i32, i16> = Some(1).into_iter()
52+
.map(|i| {
53+
let err: i8 = 1;
54+
if true { // To avoid warnings during rustfix
55+
return Err(err.into());
56+
}
57+
Ok(i)
58+
})
59+
.next()
60+
.unwrap();
61+
62+
Ok(res?)
63+
}
64+
65+
fn nested_error() -> Result<i32, i32> {
66+
Ok(1)
67+
}
68+
69+
fn main() {
70+
basic_test().unwrap();
71+
into_test().unwrap();
72+
negative_test().unwrap();
73+
closure_matches_test().unwrap();
74+
closure_into_test().unwrap();
75+
}

tests/ui/try_err.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1+
// run-rustfix
2+
13
#![deny(clippy::try_err)]
24

35
// Tests that a simple case works
46
// Should flag `Err(err)?`
57
pub fn basic_test() -> Result<i32, i32> {
68
let err: i32 = 1;
7-
Err(err)?;
9+
if true { // To avoid warnings during rustfix
10+
Err(err)?;
11+
}
812
Ok(0)
913
}
1014

1115
// Tests that `.into()` is added when appropriate
1216
pub fn into_test() -> Result<i32, i32> {
1317
let err: u8 = 1;
14-
Err(err)?;
18+
if true { // To avoid warnings during rustfix
19+
Err(err)?;
20+
}
1521
Ok(0)
1622
}
1723

@@ -28,7 +34,9 @@ pub fn closure_matches_test() -> Result<i32, i32> {
2834
let res: Result<i32, i8> = Some(1).into_iter()
2935
.map(|i| {
3036
let err: i8 = 1;
31-
Err(err)?;
37+
if true { // To avoid warnings during rustfix
38+
Err(err)?;
39+
}
3240
Ok(i)
3341
})
3442
.next()
@@ -43,7 +51,9 @@ pub fn closure_into_test() -> Result<i32, i32> {
4351
let res: Result<i32, i16> = Some(1).into_iter()
4452
.map(|i| {
4553
let err: i8 = 1;
46-
Err(err)?;
54+
if true { // To avoid warnings during rustfix
55+
Err(err)?;
56+
}
4757
Ok(i)
4858
})
4959
.next()

tests/ui/try_err.stderr

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
error: returning an `Err(_)` with the `?` operator
2-
--> $DIR/try_err.rs:7:5
2+
--> $DIR/try_err.rs:10:9
33
|
4-
LL | Err(err)?;
5-
| ^^^^^^^^^ help: try this: `return Err(err)`
4+
LL | Err(err)?;
5+
| ^^^^^^^^^ help: try this: `return Err(err)`
66
|
77
note: lint level defined here
8-
--> $DIR/try_err.rs:1:9
8+
--> $DIR/try_err.rs:3:9
99
|
1010
LL | #![deny(clippy::try_err)]
1111
| ^^^^^^^^^^^^^^^
1212

1313
error: returning an `Err(_)` with the `?` operator
14-
--> $DIR/try_err.rs:14:5
14+
--> $DIR/try_err.rs:19:9
1515
|
16-
LL | Err(err)?;
17-
| ^^^^^^^^^ help: try this: `return Err(err.into())`
16+
LL | Err(err)?;
17+
| ^^^^^^^^^ help: try this: `return Err(err.into())`
1818

1919
error: returning an `Err(_)` with the `?` operator
20-
--> $DIR/try_err.rs:31:13
20+
--> $DIR/try_err.rs:38:17
2121
|
22-
LL | Err(err)?;
23-
| ^^^^^^^^^ help: try this: `return Err(err)`
22+
LL | Err(err)?;
23+
| ^^^^^^^^^ help: try this: `return Err(err)`
2424

2525
error: returning an `Err(_)` with the `?` operator
26-
--> $DIR/try_err.rs:46:13
26+
--> $DIR/try_err.rs:55:17
2727
|
28-
LL | Err(err)?;
29-
| ^^^^^^^^^ help: try this: `return Err(err.into())`
28+
LL | Err(err)?;
29+
| ^^^^^^^^^ help: try this: `return Err(err.into())`
3030

3131
error: aborting due to 4 previous errors
3232

0 commit comments

Comments
 (0)