Skip to content

Commit 29b12f2

Browse files
committed
Auto merge of #6069 - alex-700:redundant-pattern-matching-in-macro, r=Manishearth
Forbid redundant_pattern_matching triggering in macros fixes #6065 changelog: forbid redundant_pattern_matching triggering in macros
2 parents 78fbb04 + d4f158f commit 29b12f2

File tree

6 files changed

+37
-52
lines changed

6 files changed

+37
-52
lines changed

clippy_lints/src/matches.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ impl_lint_pass!(Matches => [
502502

503503
impl<'tcx> LateLintPass<'tcx> for Matches {
504504
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
505-
if in_external_macro(cx.sess(), expr.span) {
505+
if in_external_macro(cx.sess(), expr.span) || in_macro(expr.span) {
506506
return;
507507
}
508508

tests/ui/crashes/ice-2636.rs

-22
This file was deleted.

tests/ui/crashes/ice-2636.stderr

-17
This file was deleted.

tests/ui/redundant_pattern_matching.fixed

+12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ fn main() {
4242

4343
issue5504();
4444
issue6067();
45+
issue6065();
4546

4647
let _ = if gen_res().is_ok() {
4748
1
@@ -79,6 +80,17 @@ fn issue5504() {
7980
while m!().is_some() {}
8081
}
8182

83+
fn issue6065() {
84+
macro_rules! if_let_in_macro {
85+
($pat:pat, $x:expr) => {
86+
if let Some($pat) = $x {}
87+
};
88+
}
89+
90+
// shouldn't be linted
91+
if_let_in_macro!(_, Some(42));
92+
}
93+
8294
// Methods that are unstable const should not be suggested within a const context, see issue #5697.
8395
// However, in Rust 1.48.0 the methods `is_ok` and `is_err` of `Result` were stabilized as const,
8496
// so the following should be linted.

tests/ui/redundant_pattern_matching.rs

+12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ fn main() {
5454

5555
issue5504();
5656
issue6067();
57+
issue6065();
5758

5859
let _ = if let Ok(_) = gen_res() {
5960
1
@@ -91,6 +92,17 @@ fn issue5504() {
9192
while let Some(_) = m!() {}
9293
}
9394

95+
fn issue6065() {
96+
macro_rules! if_let_in_macro {
97+
($pat:pat, $x:expr) => {
98+
if let Some($pat) = $x {}
99+
};
100+
}
101+
102+
// shouldn't be linted
103+
if_let_in_macro!(_, Some(42));
104+
}
105+
94106
// Methods that are unstable const should not be suggested within a const context, see issue #5697.
95107
// However, in Rust 1.48.0 the methods `is_ok` and `is_err` of `Result` were stabilized as const,
96108
// so the following should be linted.

tests/ui/redundant_pattern_matching.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -73,67 +73,67 @@ LL | let _ = if let Ok(_) = Ok::<usize, ()>(4) { true } else { false };
7373
| -------^^^^^--------------------- help: try this: `if Ok::<usize, ()>(4).is_ok()`
7474

7575
error: redundant pattern matching, consider using `is_ok()`
76-
--> $DIR/redundant_pattern_matching.rs:58:20
76+
--> $DIR/redundant_pattern_matching.rs:59:20
7777
|
7878
LL | let _ = if let Ok(_) = gen_res() {
7979
| -------^^^^^------------ help: try this: `if gen_res().is_ok()`
8080

8181
error: redundant pattern matching, consider using `is_err()`
82-
--> $DIR/redundant_pattern_matching.rs:60:19
82+
--> $DIR/redundant_pattern_matching.rs:61:19
8383
|
8484
LL | } else if let Err(_) = gen_res() {
8585
| -------^^^^^^------------ help: try this: `if gen_res().is_err()`
8686

8787
error: redundant pattern matching, consider using `is_some()`
88-
--> $DIR/redundant_pattern_matching.rs:83:19
88+
--> $DIR/redundant_pattern_matching.rs:84:19
8989
|
9090
LL | while let Some(_) = r#try!(result_opt()) {}
9191
| ----------^^^^^^^----------------------- help: try this: `while r#try!(result_opt()).is_some()`
9292

9393
error: redundant pattern matching, consider using `is_some()`
94-
--> $DIR/redundant_pattern_matching.rs:84:16
94+
--> $DIR/redundant_pattern_matching.rs:85:16
9595
|
9696
LL | if let Some(_) = r#try!(result_opt()) {}
9797
| -------^^^^^^^----------------------- help: try this: `if r#try!(result_opt()).is_some()`
9898

9999
error: redundant pattern matching, consider using `is_some()`
100-
--> $DIR/redundant_pattern_matching.rs:90:12
100+
--> $DIR/redundant_pattern_matching.rs:91:12
101101
|
102102
LL | if let Some(_) = m!() {}
103103
| -------^^^^^^^------- help: try this: `if m!().is_some()`
104104

105105
error: redundant pattern matching, consider using `is_some()`
106-
--> $DIR/redundant_pattern_matching.rs:91:15
106+
--> $DIR/redundant_pattern_matching.rs:92:15
107107
|
108108
LL | while let Some(_) = m!() {}
109109
| ----------^^^^^^^------- help: try this: `while m!().is_some()`
110110

111111
error: redundant pattern matching, consider using `is_ok()`
112-
--> $DIR/redundant_pattern_matching.rs:98:12
112+
--> $DIR/redundant_pattern_matching.rs:110:12
113113
|
114114
LL | if let Ok(_) = Ok::<i32, i32>(42) {}
115115
| -------^^^^^--------------------- help: try this: `if Ok::<i32, i32>(42).is_ok()`
116116

117117
error: redundant pattern matching, consider using `is_err()`
118-
--> $DIR/redundant_pattern_matching.rs:100:12
118+
--> $DIR/redundant_pattern_matching.rs:112:12
119119
|
120120
LL | if let Err(_) = Err::<i32, i32>(42) {}
121121
| -------^^^^^^---------------------- help: try this: `if Err::<i32, i32>(42).is_err()`
122122

123123
error: redundant pattern matching, consider using `is_ok()`
124-
--> $DIR/redundant_pattern_matching.rs:102:15
124+
--> $DIR/redundant_pattern_matching.rs:114:15
125125
|
126126
LL | while let Ok(_) = Ok::<i32, i32>(10) {}
127127
| ----------^^^^^--------------------- help: try this: `while Ok::<i32, i32>(10).is_ok()`
128128

129129
error: redundant pattern matching, consider using `is_err()`
130-
--> $DIR/redundant_pattern_matching.rs:104:15
130+
--> $DIR/redundant_pattern_matching.rs:116:15
131131
|
132132
LL | while let Err(_) = Ok::<i32, i32>(10) {}
133133
| ----------^^^^^^--------------------- help: try this: `while Ok::<i32, i32>(10).is_err()`
134134

135135
error: redundant pattern matching, consider using `is_ok()`
136-
--> $DIR/redundant_pattern_matching.rs:106:5
136+
--> $DIR/redundant_pattern_matching.rs:118:5
137137
|
138138
LL | / match Ok::<i32, i32>(42) {
139139
LL | | Ok(_) => true,
@@ -142,7 +142,7 @@ LL | | };
142142
| |_____^ help: try this: `Ok::<i32, i32>(42).is_ok()`
143143

144144
error: redundant pattern matching, consider using `is_err()`
145-
--> $DIR/redundant_pattern_matching.rs:111:5
145+
--> $DIR/redundant_pattern_matching.rs:123:5
146146
|
147147
LL | / match Err::<i32, i32>(42) {
148148
LL | | Ok(_) => false,

0 commit comments

Comments
 (0)