Skip to content

Commit d28dacb

Browse files
committed
redundant_pattern_matching: make rustfixable
1 parent a83a8dc commit d28dacb

4 files changed

+97
-34
lines changed

clippy_lints/src/redundant_pattern_matching.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,22 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantPatternMatching {
4949
if let ExprKind::Match(ref op, ref arms, ref match_source) = expr.node {
5050
match match_source {
5151
MatchSource::Normal => find_sugg_for_match(cx, expr, op, arms),
52-
MatchSource::IfLetDesugar { .. } => find_sugg_for_if_let(cx, expr, op, arms),
52+
MatchSource::IfLetDesugar { contains_else_clause } => {
53+
find_sugg_for_if_let(cx, expr, op, arms, *contains_else_clause)
54+
},
5355
_ => return,
5456
}
5557
}
5658
}
5759
}
5860

59-
fn find_sugg_for_if_let<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, op: &P<Expr>, arms: &HirVec<Arm>) {
61+
fn find_sugg_for_if_let<'a, 'tcx>(
62+
cx: &LateContext<'a, 'tcx>,
63+
expr: &'tcx Expr,
64+
op: &P<Expr>,
65+
arms: &HirVec<Arm>,
66+
has_else: bool,
67+
) {
6068
let good_method = match arms[0].pat.node {
6169
PatKind::TupleStruct(ref path, ref patterns, _) if patterns.len() == 1 => {
6270
if let PatKind::Wild = patterns[0].node {
@@ -79,6 +87,8 @@ fn find_sugg_for_if_let<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr,
7987
_ => return,
8088
};
8189

90+
let maybe_semi = if has_else { "" } else { ";" };
91+
8292
span_lint_and_then(
8393
cx,
8494
REDUNDANT_PATTERN_MATCHING,
@@ -89,7 +99,7 @@ fn find_sugg_for_if_let<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr,
8999
db.span_suggestion(
90100
span,
91101
"try this",
92-
format!("{}.{}", snippet(cx, op.span, "_"), good_method),
102+
format!("{}.{}{}", snippet(cx, op.span, "_"), good_method, maybe_semi),
93103
Applicability::MaybeIncorrect, // snippet
94104
);
95105
},
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::all)]
4+
#![warn(clippy::redundant_pattern_matching)]
5+
#![allow(clippy::unit_arg, clippy::let_unit_value, unused_must_use)]
6+
7+
fn main() {
8+
Ok::<i32, i32>(42).is_ok();
9+
10+
Err::<i32, i32>(42).is_err();
11+
12+
None::<()>.is_none();
13+
14+
Some(42).is_some();
15+
16+
if Ok::<i32, i32>(42).is_ok() {}
17+
18+
if Err::<i32, i32>(42).is_err() {}
19+
20+
if None::<i32>.is_none() {}
21+
22+
if Some(42).is_some() {}
23+
24+
if let Ok(x) = Ok::<i32, i32>(42) {
25+
println!("{}", x);
26+
}
27+
28+
Ok::<i32, i32>(42).is_ok();
29+
30+
Ok::<i32, i32>(42).is_err();
31+
32+
Err::<i32, i32>(42).is_err();
33+
34+
Err::<i32, i32>(42).is_ok();
35+
36+
Some(42).is_some();
37+
38+
None::<()>.is_none();
39+
40+
let _ = None::<()>.is_none();
41+
42+
let _ = Ok::<usize, ()>(4).is_ok();
43+
44+
let _ = does_something();
45+
let _ = returns_unit();
46+
47+
let opt = Some(false);
48+
let x = opt.is_some();
49+
takes_bool(x);
50+
}
51+
52+
fn takes_bool(_: bool) {}
53+
54+
fn does_something() -> bool {
55+
Ok::<i32, i32>(4).is_ok()
56+
}
57+
58+
fn returns_unit() {
59+
Ok::<i32, i32>(4).is_ok();
60+
}

tests/ui/redundant_pattern_matching.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
// run-rustfix
2+
13
#![warn(clippy::all)]
24
#![warn(clippy::redundant_pattern_matching)]
3-
#![allow(clippy::unit_arg, clippy::let_unit_value)]
5+
#![allow(clippy::unit_arg, clippy::let_unit_value, unused_must_use)]
46

57
fn main() {
68
if let Ok(_) = Ok::<i32, i32>(42) {}
@@ -66,12 +68,9 @@ fn main() {
6668
let opt = Some(false);
6769
let x = if let Some(_) = opt { true } else { false };
6870
takes_bool(x);
69-
let y = if let Some(_) = opt {};
70-
takes_unit(y);
7171
}
7272

73-
fn takes_bool(x: bool) {}
74-
fn takes_unit(x: ()) {}
73+
fn takes_bool(_: bool) {}
7574

7675
fn does_something() -> bool {
7776
if let Ok(_) = Ok::<i32, i32>(4) {
Lines changed: 20 additions & 26 deletions
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:6:12
2+
--> $DIR/redundant_pattern_matching.rs:8:12
33
|
44
LL | if let Ok(_) = Ok::<i32, i32>(42) {}
5-
| -------^^^^^------------------------ help: try this: `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:8:12
10+
--> $DIR/redundant_pattern_matching.rs:10:12
1111
|
1212
LL | if let Err(_) = Err::<i32, i32>(42) {}
13-
| -------^^^^^^------------------------- help: try this: `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:10:12
16+
--> $DIR/redundant_pattern_matching.rs:12:12
1717
|
1818
LL | if let None = None::<()> {}
19-
| -------^^^^---------------- help: try this: `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:12:12
22+
--> $DIR/redundant_pattern_matching.rs:14:12
2323
|
2424
LL | if let Some(_) = Some(42) {}
25-
| -------^^^^^^^-------------- help: try this: `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:26:5
28+
--> $DIR/redundant_pattern_matching.rs:28: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:31:5
37+
--> $DIR/redundant_pattern_matching.rs:33: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:36:5
46+
--> $DIR/redundant_pattern_matching.rs:38: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:41:5
55+
--> $DIR/redundant_pattern_matching.rs:43: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:46:5
64+
--> $DIR/redundant_pattern_matching.rs:48:5
6565
|
6666
LL | / match Some(42) {
6767
LL | | Some(_) => true,
@@ -70,7 +70,7 @@ 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:51:5
73+
--> $DIR/redundant_pattern_matching.rs:53:5
7474
|
7575
LL | / match None::<()> {
7676
LL | | Some(_) => false,
@@ -79,7 +79,7 @@ LL | | };
7979
| |_____^ help: try this: `None::<()>.is_none()`
8080

8181
error: redundant pattern matching, consider using `is_none()`
82-
--> $DIR/redundant_pattern_matching.rs:56:13
82+
--> $DIR/redundant_pattern_matching.rs:58:13
8383
|
8484
LL | let _ = match None::<()> {
8585
| _____________^
@@ -89,25 +89,19 @@ LL | | };
8989
| |_____^ help: try this: `None::<()>.is_none()`
9090

9191
error: redundant pattern matching, consider using `is_ok()`
92-
--> $DIR/redundant_pattern_matching.rs:61:20
92+
--> $DIR/redundant_pattern_matching.rs:63:20
9393
|
9494
LL | let _ = if let Ok(_) = Ok::<usize, ()>(4) { true } else { false };
9595
| -------^^^^^--------------------------------------------- help: try this: `Ok::<usize, ()>(4).is_ok()`
9696

9797
error: redundant pattern matching, consider using `is_some()`
98-
--> $DIR/redundant_pattern_matching.rs:67:20
98+
--> $DIR/redundant_pattern_matching.rs:69:20
9999
|
100100
LL | let x = if let Some(_) = opt { true } else { false };
101101
| -------^^^^^^^------------------------------ help: try this: `opt.is_some()`
102102

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-
109103
error: redundant pattern matching, consider using `is_ok()`
110-
--> $DIR/redundant_pattern_matching.rs:77:12
104+
--> $DIR/redundant_pattern_matching.rs:76:12
111105
|
112106
LL | if let Ok(_) = Ok::<i32, i32>(4) {
113107
| _____- ^^^^^
@@ -118,7 +112,7 @@ LL | | }
118112
| |_____- help: try this: `Ok::<i32, i32>(4).is_ok()`
119113

120114
error: redundant pattern matching, consider using `is_ok()`
121-
--> $DIR/redundant_pattern_matching.rs:85:12
115+
--> $DIR/redundant_pattern_matching.rs:84:12
122116
|
123117
LL | if let Ok(_) = Ok::<i32, i32>(4) {
124118
| _____- ^^^^^
@@ -128,5 +122,5 @@ LL | | false
128122
LL | | };
129123
| |_____- help: try this: `Ok::<i32, i32>(4).is_ok()`
130124

131-
error: aborting due to 16 previous errors
125+
error: aborting due to 15 previous errors
132126

0 commit comments

Comments
 (0)