Skip to content

Commit c26801e

Browse files
committed
[redundant_pattern_matching]: don't lint if if guards are present
1 parent 568ccf3 commit c26801e

File tree

4 files changed

+41
-30
lines changed

4 files changed

+41
-30
lines changed

clippy_lints/src/matches/redundant_pattern_match.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,11 @@ fn find_sugg_for_if_let<'tcx>(
199199
}
200200

201201
pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op: &Expr<'_>, arms: &[Arm<'_>]) {
202-
if arms.len() == 2 {
203-
let node_pair = (&arms[0].pat.kind, &arms[1].pat.kind);
202+
if let [arm1, arm2] = arms
203+
&& arm1.guard.is_none()
204+
&& arm2.guard.is_none()
205+
{
206+
let node_pair = (&arm1.pat.kind, &arm2.pat.kind);
204207

205208
if let Some(good_method) = found_good_method(cx, arms, node_pair) {
206209
let span = is_expn_of(expr.span, "matches").unwrap_or(expr.span.to(op.span));

tests/ui/redundant_pattern_matching_option.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
clippy::if_same_then_else
1212
)]
1313

14+
fn issue_11174<T>(boolean: bool, maybe_some: Option<T>) -> bool {
15+
matches!(maybe_some, None if !boolean)
16+
}
17+
1418
fn main() {
1519
if None::<()>.is_none() {}
1620

tests/ui/redundant_pattern_matching_option.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
clippy::if_same_then_else
1212
)]
1313

14+
fn issue_11174<T>(boolean: bool, maybe_some: Option<T>) -> bool {
15+
matches!(maybe_some, None if !boolean)
16+
}
17+
1418
fn main() {
1519
if let None = None::<()> {}
1620

tests/ui/redundant_pattern_matching_option.stderr

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
error: redundant pattern matching, consider using `is_none()`
2-
--> $DIR/redundant_pattern_matching_option.rs:15:12
2+
--> $DIR/redundant_pattern_matching_option.rs:19:12
33
|
44
LL | if let None = None::<()> {}
55
| -------^^^^------------- help: try: `if None::<()>.is_none()`
66
|
77
= note: `-D clippy::redundant-pattern-matching` implied by `-D warnings`
88

99
error: redundant pattern matching, consider using `is_some()`
10-
--> $DIR/redundant_pattern_matching_option.rs:17:12
10+
--> $DIR/redundant_pattern_matching_option.rs:21:12
1111
|
1212
LL | if let Some(_) = Some(42) {}
1313
| -------^^^^^^^----------- help: try: `if Some(42).is_some()`
1414

1515
error: redundant pattern matching, consider using `is_some()`
16-
--> $DIR/redundant_pattern_matching_option.rs:19:12
16+
--> $DIR/redundant_pattern_matching_option.rs:23:12
1717
|
1818
LL | if let Some(_) = Some(42) {
1919
| -------^^^^^^^----------- help: try: `if Some(42).is_some()`
2020

2121
error: redundant pattern matching, consider using `is_some()`
22-
--> $DIR/redundant_pattern_matching_option.rs:25:15
22+
--> $DIR/redundant_pattern_matching_option.rs:29:15
2323
|
2424
LL | while let Some(_) = Some(42) {}
2525
| ----------^^^^^^^----------- help: try: `while Some(42).is_some()`
2626

2727
error: redundant pattern matching, consider using `is_none()`
28-
--> $DIR/redundant_pattern_matching_option.rs:27:15
28+
--> $DIR/redundant_pattern_matching_option.rs:31:15
2929
|
3030
LL | while let None = Some(42) {}
3131
| ----------^^^^----------- help: try: `while Some(42).is_none()`
3232

3333
error: redundant pattern matching, consider using `is_none()`
34-
--> $DIR/redundant_pattern_matching_option.rs:29:15
34+
--> $DIR/redundant_pattern_matching_option.rs:33:15
3535
|
3636
LL | while let None = None::<()> {}
3737
| ----------^^^^------------- help: try: `while None::<()>.is_none()`
3838

3939
error: redundant pattern matching, consider using `is_some()`
40-
--> $DIR/redundant_pattern_matching_option.rs:32:15
40+
--> $DIR/redundant_pattern_matching_option.rs:36:15
4141
|
4242
LL | while let Some(_) = v.pop() {
4343
| ----------^^^^^^^---------- help: try: `while v.pop().is_some()`
4444

4545
error: redundant pattern matching, consider using `is_some()`
46-
--> $DIR/redundant_pattern_matching_option.rs:40:5
46+
--> $DIR/redundant_pattern_matching_option.rs:44:5
4747
|
4848
LL | / match Some(42) {
4949
LL | | Some(_) => true,
@@ -52,7 +52,7 @@ LL | | };
5252
| |_____^ help: try: `Some(42).is_some()`
5353

5454
error: redundant pattern matching, consider using `is_none()`
55-
--> $DIR/redundant_pattern_matching_option.rs:45:5
55+
--> $DIR/redundant_pattern_matching_option.rs:49:5
5656
|
5757
LL | / match None::<()> {
5858
LL | | Some(_) => false,
@@ -61,7 +61,7 @@ LL | | };
6161
| |_____^ help: try: `None::<()>.is_none()`
6262

6363
error: redundant pattern matching, consider using `is_none()`
64-
--> $DIR/redundant_pattern_matching_option.rs:50:13
64+
--> $DIR/redundant_pattern_matching_option.rs:54:13
6565
|
6666
LL | let _ = match None::<()> {
6767
| _____________^
@@ -71,55 +71,55 @@ LL | | };
7171
| |_____^ help: try: `None::<()>.is_none()`
7272

7373
error: redundant pattern matching, consider using `is_some()`
74-
--> $DIR/redundant_pattern_matching_option.rs:56:20
74+
--> $DIR/redundant_pattern_matching_option.rs:60:20
7575
|
7676
LL | let _ = if let Some(_) = opt { true } else { false };
7777
| -------^^^^^^^------ help: try: `if opt.is_some()`
7878

7979
error: redundant pattern matching, consider using `is_some()`
80-
--> $DIR/redundant_pattern_matching_option.rs:62:20
80+
--> $DIR/redundant_pattern_matching_option.rs:66:20
8181
|
8282
LL | let _ = if let Some(_) = gen_opt() {
8383
| -------^^^^^^^------------ help: try: `if gen_opt().is_some()`
8484

8585
error: redundant pattern matching, consider using `is_none()`
86-
--> $DIR/redundant_pattern_matching_option.rs:64:19
86+
--> $DIR/redundant_pattern_matching_option.rs:68:19
8787
|
8888
LL | } else if let None = gen_opt() {
8989
| -------^^^^------------ help: try: `if gen_opt().is_none()`
9090

9191
error: redundant pattern matching, consider using `is_some()`
92-
--> $DIR/redundant_pattern_matching_option.rs:70:12
92+
--> $DIR/redundant_pattern_matching_option.rs:74:12
9393
|
9494
LL | if let Some(..) = gen_opt() {}
9595
| -------^^^^^^^^------------ help: try: `if gen_opt().is_some()`
9696

9797
error: redundant pattern matching, consider using `is_some()`
98-
--> $DIR/redundant_pattern_matching_option.rs:85:12
98+
--> $DIR/redundant_pattern_matching_option.rs:89:12
9999
|
100100
LL | if let Some(_) = Some(42) {}
101101
| -------^^^^^^^----------- help: try: `if Some(42).is_some()`
102102

103103
error: redundant pattern matching, consider using `is_none()`
104-
--> $DIR/redundant_pattern_matching_option.rs:87:12
104+
--> $DIR/redundant_pattern_matching_option.rs:91:12
105105
|
106106
LL | if let None = None::<()> {}
107107
| -------^^^^------------- help: try: `if None::<()>.is_none()`
108108

109109
error: redundant pattern matching, consider using `is_some()`
110-
--> $DIR/redundant_pattern_matching_option.rs:89:15
110+
--> $DIR/redundant_pattern_matching_option.rs:93:15
111111
|
112112
LL | while let Some(_) = Some(42) {}
113113
| ----------^^^^^^^----------- help: try: `while Some(42).is_some()`
114114

115115
error: redundant pattern matching, consider using `is_none()`
116-
--> $DIR/redundant_pattern_matching_option.rs:91:15
116+
--> $DIR/redundant_pattern_matching_option.rs:95:15
117117
|
118118
LL | while let None = None::<()> {}
119119
| ----------^^^^------------- help: try: `while None::<()>.is_none()`
120120

121121
error: redundant pattern matching, consider using `is_some()`
122-
--> $DIR/redundant_pattern_matching_option.rs:93:5
122+
--> $DIR/redundant_pattern_matching_option.rs:97:5
123123
|
124124
LL | / match Some(42) {
125125
LL | | Some(_) => true,
@@ -128,7 +128,7 @@ LL | | };
128128
| |_____^ help: try: `Some(42).is_some()`
129129

130130
error: redundant pattern matching, consider using `is_none()`
131-
--> $DIR/redundant_pattern_matching_option.rs:98:5
131+
--> $DIR/redundant_pattern_matching_option.rs:102:5
132132
|
133133
LL | / match None::<()> {
134134
LL | | Some(_) => false,
@@ -137,19 +137,19 @@ LL | | };
137137
| |_____^ help: try: `None::<()>.is_none()`
138138

139139
error: redundant pattern matching, consider using `is_none()`
140-
--> $DIR/redundant_pattern_matching_option.rs:106:12
140+
--> $DIR/redundant_pattern_matching_option.rs:110:12
141141
|
142142
LL | if let None = *(&None::<()>) {}
143143
| -------^^^^----------------- help: try: `if (&None::<()>).is_none()`
144144

145145
error: redundant pattern matching, consider using `is_none()`
146-
--> $DIR/redundant_pattern_matching_option.rs:107:12
146+
--> $DIR/redundant_pattern_matching_option.rs:111:12
147147
|
148148
LL | if let None = *&None::<()> {}
149149
| -------^^^^--------------- help: try: `if (&None::<()>).is_none()`
150150

151151
error: redundant pattern matching, consider using `is_some()`
152-
--> $DIR/redundant_pattern_matching_option.rs:113:5
152+
--> $DIR/redundant_pattern_matching_option.rs:117:5
153153
|
154154
LL | / match x {
155155
LL | | Some(_) => true,
@@ -158,7 +158,7 @@ LL | | };
158158
| |_____^ help: try: `x.is_some()`
159159

160160
error: redundant pattern matching, consider using `is_none()`
161-
--> $DIR/redundant_pattern_matching_option.rs:118:5
161+
--> $DIR/redundant_pattern_matching_option.rs:122:5
162162
|
163163
LL | / match x {
164164
LL | | None => true,
@@ -167,7 +167,7 @@ LL | | };
167167
| |_____^ help: try: `x.is_none()`
168168

169169
error: redundant pattern matching, consider using `is_none()`
170-
--> $DIR/redundant_pattern_matching_option.rs:123:5
170+
--> $DIR/redundant_pattern_matching_option.rs:127:5
171171
|
172172
LL | / match x {
173173
LL | | Some(_) => false,
@@ -176,7 +176,7 @@ LL | | };
176176
| |_____^ help: try: `x.is_none()`
177177

178178
error: redundant pattern matching, consider using `is_some()`
179-
--> $DIR/redundant_pattern_matching_option.rs:128:5
179+
--> $DIR/redundant_pattern_matching_option.rs:132:5
180180
|
181181
LL | / match x {
182182
LL | | None => false,
@@ -185,13 +185,13 @@ LL | | };
185185
| |_____^ help: try: `x.is_some()`
186186

187187
error: redundant pattern matching, consider using `is_some()`
188-
--> $DIR/redundant_pattern_matching_option.rs:143:13
188+
--> $DIR/redundant_pattern_matching_option.rs:147:13
189189
|
190190
LL | let _ = matches!(x, Some(_));
191191
| ^^^^^^^^^^^^^^^^^^^^ help: try: `x.is_some()`
192192

193193
error: redundant pattern matching, consider using `is_none()`
194-
--> $DIR/redundant_pattern_matching_option.rs:145:13
194+
--> $DIR/redundant_pattern_matching_option.rs:149:13
195195
|
196196
LL | let _ = matches!(x, None);
197197
| ^^^^^^^^^^^^^^^^^ help: try: `x.is_none()`

0 commit comments

Comments
 (0)