Skip to content

Commit 8cf4219

Browse files
committed
Auto merge of #5815 - JarredAllen:redundant_pattern_bugfix, r=flip1995
Redundant pattern bugfix changelog: Fixes the bug reported #5766
2 parents 799eef6 + e85b590 commit 8cf4219

File tree

4 files changed

+54
-34
lines changed

4 files changed

+54
-34
lines changed

clippy_lints/src/matches.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,10 @@ mod redundant_pattern_match {
15121512
}
15131513
}
15141514

1515+
let result_expr = match &op.kind {
1516+
ExprKind::AddrOf(_, _, borrowed) => borrowed,
1517+
_ => op,
1518+
};
15151519
span_lint_and_then(
15161520
cx,
15171521
REDUNDANT_PATTERN_MATCHING,
@@ -1524,7 +1528,7 @@ mod redundant_pattern_match {
15241528

15251529
// while let ... = ... { ... }
15261530
// ^^^
1527-
let op_span = op.span.source_callsite();
1531+
let op_span = result_expr.span.source_callsite();
15281532

15291533
// while let ... = ... { ... }
15301534
// ^^^^^^^^^^^^^^^^^^^
@@ -1589,17 +1593,21 @@ mod redundant_pattern_match {
15891593
};
15901594

15911595
if let Some(good_method) = found_good_method {
1596+
let span = expr.span.to(op.span);
1597+
let result_expr = match &op.kind {
1598+
ExprKind::AddrOf(_, _, borrowed) => borrowed,
1599+
_ => op,
1600+
};
15921601
span_lint_and_then(
15931602
cx,
15941603
REDUNDANT_PATTERN_MATCHING,
15951604
expr.span,
15961605
&format!("redundant pattern matching, consider using `{}`", good_method),
15971606
|diag| {
1598-
let span = expr.span.to(op.span);
15991607
diag.span_suggestion(
16001608
span,
16011609
"try this",
1602-
format!("{}.{}", snippet(cx, op.span, "_"), good_method),
1610+
format!("{}.{}", snippet(cx, result_expr.span, "_"), good_method),
16031611
Applicability::MaybeIncorrect, // snippet
16041612
);
16051613
},

tests/ui/redundant_pattern_matching.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
)]
1212

1313
fn main() {
14+
let result: Result<usize, usize> = Err(5);
15+
if result.is_ok() {}
16+
1417
if Ok::<i32, i32>(42).is_ok() {}
1518

1619
if Err::<i32, i32>(42).is_err() {}

tests/ui/redundant_pattern_matching.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
)]
1212

1313
fn main() {
14+
let result: Result<usize, usize> = Err(5);
15+
if let Ok(_) = &result {}
16+
1417
if let Ok(_) = Ok::<i32, i32>(42) {}
1518

1619
if let Err(_) = Err::<i32, i32>(42) {}
Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,79 @@
11
error: redundant pattern matching, consider using `is_ok()`
2-
--> $DIR/redundant_pattern_matching.rs:14:12
2+
--> $DIR/redundant_pattern_matching.rs:15:12
33
|
4-
LL | if let Ok(_) = Ok::<i32, i32>(42) {}
5-
| -------^^^^^--------------------- help: try this: `if Ok::<i32, i32>(42).is_ok()`
4+
LL | if let Ok(_) = &result {}
5+
| -------^^^^^---------- help: try this: `if result.is_ok()`
66
|
77
= note: `-D clippy::redundant-pattern-matching` implied by `-D warnings`
88

9+
error: redundant pattern matching, consider using `is_ok()`
10+
--> $DIR/redundant_pattern_matching.rs:17:12
11+
|
12+
LL | if let Ok(_) = Ok::<i32, i32>(42) {}
13+
| -------^^^^^--------------------- help: try this: `if Ok::<i32, i32>(42).is_ok()`
14+
915
error: redundant pattern matching, consider using `is_err()`
10-
--> $DIR/redundant_pattern_matching.rs:16:12
16+
--> $DIR/redundant_pattern_matching.rs:19:12
1117
|
1218
LL | if let Err(_) = Err::<i32, i32>(42) {}
1319
| -------^^^^^^---------------------- help: try this: `if Err::<i32, i32>(42).is_err()`
1420

1521
error: redundant pattern matching, consider using `is_none()`
16-
--> $DIR/redundant_pattern_matching.rs:18:12
22+
--> $DIR/redundant_pattern_matching.rs:21:12
1723
|
1824
LL | if let None = None::<()> {}
1925
| -------^^^^------------- help: try this: `if None::<()>.is_none()`
2026

2127
error: redundant pattern matching, consider using `is_some()`
22-
--> $DIR/redundant_pattern_matching.rs:20:12
28+
--> $DIR/redundant_pattern_matching.rs:23:12
2329
|
2430
LL | if let Some(_) = Some(42) {}
2531
| -------^^^^^^^----------- help: try this: `if Some(42).is_some()`
2632

2733
error: redundant pattern matching, consider using `is_some()`
28-
--> $DIR/redundant_pattern_matching.rs:22:12
34+
--> $DIR/redundant_pattern_matching.rs:25:12
2935
|
3036
LL | if let Some(_) = Some(42) {
3137
| -------^^^^^^^----------- help: try this: `if Some(42).is_some()`
3238

3339
error: redundant pattern matching, consider using `is_some()`
34-
--> $DIR/redundant_pattern_matching.rs:28:15
40+
--> $DIR/redundant_pattern_matching.rs:31:15
3541
|
3642
LL | while let Some(_) = Some(42) {}
3743
| ----------^^^^^^^----------- help: try this: `while Some(42).is_some()`
3844

3945
error: redundant pattern matching, consider using `is_none()`
40-
--> $DIR/redundant_pattern_matching.rs:30:15
46+
--> $DIR/redundant_pattern_matching.rs:33:15
4147
|
4248
LL | while let None = Some(42) {}
4349
| ----------^^^^----------- help: try this: `while Some(42).is_none()`
4450

4551
error: redundant pattern matching, consider using `is_none()`
46-
--> $DIR/redundant_pattern_matching.rs:32:15
52+
--> $DIR/redundant_pattern_matching.rs:35:15
4753
|
4854
LL | while let None = None::<()> {}
4955
| ----------^^^^------------- help: try this: `while None::<()>.is_none()`
5056

5157
error: redundant pattern matching, consider using `is_ok()`
52-
--> $DIR/redundant_pattern_matching.rs:34:15
58+
--> $DIR/redundant_pattern_matching.rs:37:15
5359
|
5460
LL | while let Ok(_) = Ok::<i32, i32>(10) {}
5561
| ----------^^^^^--------------------- help: try this: `while Ok::<i32, i32>(10).is_ok()`
5662

5763
error: redundant pattern matching, consider using `is_err()`
58-
--> $DIR/redundant_pattern_matching.rs:36:15
64+
--> $DIR/redundant_pattern_matching.rs:39:15
5965
|
6066
LL | while let Err(_) = Ok::<i32, i32>(10) {}
6167
| ----------^^^^^^--------------------- help: try this: `while Ok::<i32, i32>(10).is_err()`
6268

6369
error: redundant pattern matching, consider using `is_some()`
64-
--> $DIR/redundant_pattern_matching.rs:39:15
70+
--> $DIR/redundant_pattern_matching.rs:42:15
6571
|
6672
LL | while let Some(_) = v.pop() {
6773
| ----------^^^^^^^---------- help: try this: `while v.pop().is_some()`
6874

6975
error: redundant pattern matching, consider using `is_ok()`
70-
--> $DIR/redundant_pattern_matching.rs:55:5
76+
--> $DIR/redundant_pattern_matching.rs:58:5
7177
|
7278
LL | / match Ok::<i32, i32>(42) {
7379
LL | | Ok(_) => true,
@@ -76,7 +82,7 @@ LL | | };
7682
| |_____^ help: try this: `Ok::<i32, i32>(42).is_ok()`
7783

7884
error: redundant pattern matching, consider using `is_err()`
79-
--> $DIR/redundant_pattern_matching.rs:60:5
85+
--> $DIR/redundant_pattern_matching.rs:63:5
8086
|
8187
LL | / match Ok::<i32, i32>(42) {
8288
LL | | Ok(_) => false,
@@ -85,7 +91,7 @@ LL | | };
8591
| |_____^ help: try this: `Ok::<i32, i32>(42).is_err()`
8692

8793
error: redundant pattern matching, consider using `is_err()`
88-
--> $DIR/redundant_pattern_matching.rs:65:5
94+
--> $DIR/redundant_pattern_matching.rs:68:5
8995
|
9096
LL | / match Err::<i32, i32>(42) {
9197
LL | | Ok(_) => false,
@@ -94,7 +100,7 @@ LL | | };
94100
| |_____^ help: try this: `Err::<i32, i32>(42).is_err()`
95101

96102
error: redundant pattern matching, consider using `is_ok()`
97-
--> $DIR/redundant_pattern_matching.rs:70:5
103+
--> $DIR/redundant_pattern_matching.rs:73:5
98104
|
99105
LL | / match Err::<i32, i32>(42) {
100106
LL | | Ok(_) => true,
@@ -103,7 +109,7 @@ LL | | };
103109
| |_____^ help: try this: `Err::<i32, i32>(42).is_ok()`
104110

105111
error: redundant pattern matching, consider using `is_some()`
106-
--> $DIR/redundant_pattern_matching.rs:75:5
112+
--> $DIR/redundant_pattern_matching.rs:78:5
107113
|
108114
LL | / match Some(42) {
109115
LL | | Some(_) => true,
@@ -112,7 +118,7 @@ LL | | };
112118
| |_____^ help: try this: `Some(42).is_some()`
113119

114120
error: redundant pattern matching, consider using `is_none()`
115-
--> $DIR/redundant_pattern_matching.rs:80:5
121+
--> $DIR/redundant_pattern_matching.rs:83:5
116122
|
117123
LL | / match None::<()> {
118124
LL | | Some(_) => false,
@@ -121,7 +127,7 @@ LL | | };
121127
| |_____^ help: try this: `None::<()>.is_none()`
122128

123129
error: redundant pattern matching, consider using `is_none()`
124-
--> $DIR/redundant_pattern_matching.rs:85:13
130+
--> $DIR/redundant_pattern_matching.rs:88:13
125131
|
126132
LL | let _ = match None::<()> {
127133
| _____________^
@@ -131,64 +137,64 @@ LL | | };
131137
| |_____^ help: try this: `None::<()>.is_none()`
132138

133139
error: redundant pattern matching, consider using `is_ok()`
134-
--> $DIR/redundant_pattern_matching.rs:90:20
140+
--> $DIR/redundant_pattern_matching.rs:93:20
135141
|
136142
LL | let _ = if let Ok(_) = Ok::<usize, ()>(4) { true } else { false };
137143
| -------^^^^^--------------------- help: try this: `if Ok::<usize, ()>(4).is_ok()`
138144

139145
error: redundant pattern matching, consider using `is_some()`
140-
--> $DIR/redundant_pattern_matching.rs:93:20
146+
--> $DIR/redundant_pattern_matching.rs:96:20
141147
|
142148
LL | let x = if let Some(_) = opt { true } else { false };
143149
| -------^^^^^^^------ help: try this: `if opt.is_some()`
144150

145151
error: redundant pattern matching, consider using `is_some()`
146-
--> $DIR/redundant_pattern_matching.rs:99:20
152+
--> $DIR/redundant_pattern_matching.rs:102:20
147153
|
148154
LL | let _ = if let Some(_) = gen_opt() {
149155
| -------^^^^^^^------------ help: try this: `if gen_opt().is_some()`
150156

151157
error: redundant pattern matching, consider using `is_none()`
152-
--> $DIR/redundant_pattern_matching.rs:101:19
158+
--> $DIR/redundant_pattern_matching.rs:104:19
153159
|
154160
LL | } else if let None = gen_opt() {
155161
| -------^^^^------------ help: try this: `if gen_opt().is_none()`
156162

157163
error: redundant pattern matching, consider using `is_ok()`
158-
--> $DIR/redundant_pattern_matching.rs:103:19
164+
--> $DIR/redundant_pattern_matching.rs:106:19
159165
|
160166
LL | } else if let Ok(_) = gen_res() {
161167
| -------^^^^^------------ help: try this: `if gen_res().is_ok()`
162168

163169
error: redundant pattern matching, consider using `is_err()`
164-
--> $DIR/redundant_pattern_matching.rs:105:19
170+
--> $DIR/redundant_pattern_matching.rs:108:19
165171
|
166172
LL | } else if let Err(_) = gen_res() {
167173
| -------^^^^^^------------ help: try this: `if gen_res().is_err()`
168174

169175
error: redundant pattern matching, consider using `is_some()`
170-
--> $DIR/redundant_pattern_matching.rs:138:19
176+
--> $DIR/redundant_pattern_matching.rs:141:19
171177
|
172178
LL | while let Some(_) = r#try!(result_opt()) {}
173179
| ----------^^^^^^^----------------------- help: try this: `while r#try!(result_opt()).is_some()`
174180

175181
error: redundant pattern matching, consider using `is_some()`
176-
--> $DIR/redundant_pattern_matching.rs:139:16
182+
--> $DIR/redundant_pattern_matching.rs:142:16
177183
|
178184
LL | if let Some(_) = r#try!(result_opt()) {}
179185
| -------^^^^^^^----------------------- help: try this: `if r#try!(result_opt()).is_some()`
180186

181187
error: redundant pattern matching, consider using `is_some()`
182-
--> $DIR/redundant_pattern_matching.rs:145:12
188+
--> $DIR/redundant_pattern_matching.rs:148:12
183189
|
184190
LL | if let Some(_) = m!() {}
185191
| -------^^^^^^^------- help: try this: `if m!().is_some()`
186192

187193
error: redundant pattern matching, consider using `is_some()`
188-
--> $DIR/redundant_pattern_matching.rs:146:15
194+
--> $DIR/redundant_pattern_matching.rs:149:15
189195
|
190196
LL | while let Some(_) = m!() {}
191197
| ----------^^^^^^^------- help: try this: `while m!().is_some()`
192198

193-
error: aborting due to 28 previous errors
199+
error: aborting due to 29 previous errors
194200

0 commit comments

Comments
 (0)