Skip to content

Commit 57c67a2

Browse files
committed
Auto merge of #4352 - phansch:fix_redundant_pattern_matching, r=flip1995
Fix some suggestions for redundant_pattern_matching .. and change the Applicability to `MaybeIncorrect`. Fixes the problem displayed in #4344 (comment). We now append `{}` to the suggestion so that the conditional has the correct syntax again. (If we were to _remove_ the `if` instead, it would trigger the `unused_must_use` warning for `#[must_use]` types.) changelog: Fix some suggestions for `redundant_pattern_matching`
2 parents 45d24fd + 436d429 commit 57c67a2

File tree

3 files changed

+104
-18
lines changed

3 files changed

+104
-18
lines changed

clippy_lints/src/redundant_pattern_matching.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ fn find_sugg_for_if_let<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr,
9090
db.span_suggestion(
9191
span,
9292
"try this",
93-
format!("if {}.{}", snippet(cx, op.span, "_"), good_method),
94-
Applicability::MachineApplicable, // snippet
93+
format!("{}.{}", snippet(cx, op.span, "_"), good_method),
94+
Applicability::MaybeIncorrect, // snippet
9595
);
9696
},
9797
);
@@ -154,7 +154,7 @@ fn find_sugg_for_match<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, o
154154
span,
155155
"try this",
156156
format!("{}.{}", snippet(cx, op.span, "_"), good_method),
157-
Applicability::MachineApplicable, // snippet
157+
Applicability::MaybeIncorrect, // snippet
158158
);
159159
},
160160
);

tests/ui/redundant_pattern_matching.rs

+36
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![warn(clippy::all)]
22
#![warn(clippy::redundant_pattern_matching)]
3+
#![allow(clippy::unit_arg, clippy::let_unit_value)]
34

45
fn main() {
56
if let Ok(_) = Ok::<i32, i32>(42) {}
@@ -51,4 +52,39 @@ fn main() {
5152
Some(_) => false,
5253
None => true,
5354
};
55+
56+
let _ = match None::<()> {
57+
Some(_) => false,
58+
None => true,
59+
};
60+
61+
let _ = if let Ok(_) = Ok::<usize, ()>(4) { true } else { false };
62+
63+
let _ = does_something();
64+
let _ = returns_unit();
65+
66+
let opt = Some(false);
67+
let x = if let Some(_) = opt { true } else { false };
68+
takes_bool(x);
69+
let y = if let Some(_) = opt {};
70+
takes_unit(y);
71+
}
72+
73+
fn takes_bool(x: bool) {}
74+
fn takes_unit(x: ()) {}
75+
76+
fn does_something() -> bool {
77+
if let Ok(_) = Ok::<i32, i32>(4) {
78+
true
79+
} else {
80+
false
81+
}
82+
}
83+
84+
fn returns_unit() {
85+
if let Ok(_) = Ok::<i32, i32>(4) {
86+
true
87+
} else {
88+
false
89+
};
5490
}
+65-15
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
error: redundant pattern matching, consider using `is_ok()`
2-
--> $DIR/redundant_pattern_matching.rs:5:12
2+
--> $DIR/redundant_pattern_matching.rs:6:12
33
|
44
LL | if let Ok(_) = Ok::<i32, i32>(42) {}
5-
| -------^^^^^------------------------ help: try this: `if Ok::<i32, i32>(42).is_ok()`
5+
| -------^^^^^------------------------ help: try this: `Ok::<i32, i32>(42).is_ok()`
66
|
77
= note: `-D clippy::redundant-pattern-matching` implied by `-D warnings`
88

99
error: redundant pattern matching, consider using `is_err()`
10-
--> $DIR/redundant_pattern_matching.rs:7:12
10+
--> $DIR/redundant_pattern_matching.rs:8:12
1111
|
1212
LL | if let Err(_) = Err::<i32, i32>(42) {}
13-
| -------^^^^^^------------------------- help: try this: `if Err::<i32, i32>(42).is_err()`
13+
| -------^^^^^^------------------------- help: try this: `Err::<i32, i32>(42).is_err()`
1414

1515
error: redundant pattern matching, consider using `is_none()`
16-
--> $DIR/redundant_pattern_matching.rs:9:12
16+
--> $DIR/redundant_pattern_matching.rs:10:12
1717
|
1818
LL | if let None = None::<()> {}
19-
| -------^^^^---------------- help: try this: `if None::<()>.is_none()`
19+
| -------^^^^---------------- help: try this: `None::<()>.is_none()`
2020

2121
error: redundant pattern matching, consider using `is_some()`
22-
--> $DIR/redundant_pattern_matching.rs:11:12
22+
--> $DIR/redundant_pattern_matching.rs:12:12
2323
|
2424
LL | if let Some(_) = Some(42) {}
25-
| -------^^^^^^^-------------- help: try this: `if Some(42).is_some()`
25+
| -------^^^^^^^-------------- help: try this: `Some(42).is_some()`
2626

2727
error: redundant pattern matching, consider using `is_ok()`
28-
--> $DIR/redundant_pattern_matching.rs:25:5
28+
--> $DIR/redundant_pattern_matching.rs:26:5
2929
|
3030
LL | / match Ok::<i32, i32>(42) {
3131
LL | | Ok(_) => true,
@@ -34,7 +34,7 @@ LL | | };
3434
| |_____^ help: try this: `Ok::<i32, i32>(42).is_ok()`
3535

3636
error: redundant pattern matching, consider using `is_err()`
37-
--> $DIR/redundant_pattern_matching.rs:30:5
37+
--> $DIR/redundant_pattern_matching.rs:31:5
3838
|
3939
LL | / match Ok::<i32, i32>(42) {
4040
LL | | Ok(_) => false,
@@ -43,7 +43,7 @@ LL | | };
4343
| |_____^ help: try this: `Ok::<i32, i32>(42).is_err()`
4444

4545
error: redundant pattern matching, consider using `is_err()`
46-
--> $DIR/redundant_pattern_matching.rs:35:5
46+
--> $DIR/redundant_pattern_matching.rs:36:5
4747
|
4848
LL | / match Err::<i32, i32>(42) {
4949
LL | | Ok(_) => false,
@@ -52,7 +52,7 @@ LL | | };
5252
| |_____^ help: try this: `Err::<i32, i32>(42).is_err()`
5353

5454
error: redundant pattern matching, consider using `is_ok()`
55-
--> $DIR/redundant_pattern_matching.rs:40:5
55+
--> $DIR/redundant_pattern_matching.rs:41:5
5656
|
5757
LL | / match Err::<i32, i32>(42) {
5858
LL | | Ok(_) => true,
@@ -61,7 +61,7 @@ LL | | };
6161
| |_____^ help: try this: `Err::<i32, i32>(42).is_ok()`
6262

6363
error: redundant pattern matching, consider using `is_some()`
64-
--> $DIR/redundant_pattern_matching.rs:45:5
64+
--> $DIR/redundant_pattern_matching.rs:46:5
6565
|
6666
LL | / match Some(42) {
6767
LL | | Some(_) => true,
@@ -70,13 +70,63 @@ LL | | };
7070
| |_____^ help: try this: `Some(42).is_some()`
7171

7272
error: redundant pattern matching, consider using `is_none()`
73-
--> $DIR/redundant_pattern_matching.rs:50:5
73+
--> $DIR/redundant_pattern_matching.rs:51:5
7474
|
7575
LL | / match None::<()> {
7676
LL | | Some(_) => false,
7777
LL | | None => true,
7878
LL | | };
7979
| |_____^ help: try this: `None::<()>.is_none()`
8080

81-
error: aborting due to 10 previous errors
81+
error: redundant pattern matching, consider using `is_none()`
82+
--> $DIR/redundant_pattern_matching.rs:56:13
83+
|
84+
LL | let _ = match None::<()> {
85+
| _____________^
86+
LL | | Some(_) => false,
87+
LL | | None => true,
88+
LL | | };
89+
| |_____^ help: try this: `None::<()>.is_none()`
90+
91+
error: redundant pattern matching, consider using `is_ok()`
92+
--> $DIR/redundant_pattern_matching.rs:61:20
93+
|
94+
LL | let _ = if let Ok(_) = Ok::<usize, ()>(4) { true } else { false };
95+
| -------^^^^^--------------------------------------------- help: try this: `Ok::<usize, ()>(4).is_ok()`
96+
97+
error: redundant pattern matching, consider using `is_some()`
98+
--> $DIR/redundant_pattern_matching.rs:67:20
99+
|
100+
LL | let x = if let Some(_) = opt { true } else { false };
101+
| -------^^^^^^^------------------------------ help: try this: `opt.is_some()`
102+
103+
error: redundant pattern matching, consider using `is_some()`
104+
--> $DIR/redundant_pattern_matching.rs:69:20
105+
|
106+
LL | let y = if let Some(_) = opt {};
107+
| -------^^^^^^^--------- help: try this: `opt.is_some()`
108+
109+
error: redundant pattern matching, consider using `is_ok()`
110+
--> $DIR/redundant_pattern_matching.rs:77:12
111+
|
112+
LL | if let Ok(_) = Ok::<i32, i32>(4) {
113+
| _____- ^^^^^
114+
LL | | true
115+
LL | | } else {
116+
LL | | false
117+
LL | | }
118+
| |_____- help: try this: `Ok::<i32, i32>(4).is_ok()`
119+
120+
error: redundant pattern matching, consider using `is_ok()`
121+
--> $DIR/redundant_pattern_matching.rs:85:12
122+
|
123+
LL | if let Ok(_) = Ok::<i32, i32>(4) {
124+
| _____- ^^^^^
125+
LL | | true
126+
LL | | } else {
127+
LL | | false
128+
LL | | };
129+
| |_____- help: try this: `Ok::<i32, i32>(4).is_ok()`
130+
131+
error: aborting due to 16 previous errors
82132

0 commit comments

Comments
 (0)