Skip to content

Commit ca35e81

Browse files
committed
Forbid redundant_pattern_matching triggering in macros
1 parent d88b9b7 commit ca35e81

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
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/redundant_pattern_matching.fixed

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

7878
issue5504();
7979
issue5697();
80+
issue6065();
8081

8182
let _ = if gen_opt().is_some() {
8283
1
@@ -128,6 +129,17 @@ fn issue5504() {
128129
while m!().is_some() {}
129130
}
130131

132+
fn issue6065() {
133+
macro_rules! if_let_in_macro {
134+
($pat:pat, $x:expr) => {
135+
if let Some($pat) = $x {}
136+
};
137+
}
138+
139+
// shouldn't be linted
140+
if_let_in_macro!(_, Some(42));
141+
}
142+
131143
// None of these should be linted because none of the suggested methods
132144
// are `const fn` without toggling a feature.
133145
const fn issue5697() {

tests/ui/redundant_pattern_matching.rs

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

9999
issue5504();
100100
issue5697();
101+
issue6065();
101102

102103
let _ = if let Some(_) = gen_opt() {
103104
1
@@ -149,6 +150,17 @@ fn issue5504() {
149150
while let Some(_) = m!() {}
150151
}
151152

153+
fn issue6065() {
154+
macro_rules! if_let_in_macro {
155+
($pat:pat, $x:expr) => {
156+
if let Some($pat) = $x {}
157+
};
158+
}
159+
160+
// shouldn't be linted
161+
if_let_in_macro!(_, Some(42));
162+
}
163+
152164
// None of these should be linted because none of the suggested methods
153165
// are `const fn` without toggling a feature.
154166
const fn issue5697() {

tests/ui/redundant_pattern_matching.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -149,49 +149,49 @@ LL | let x = if let Some(_) = opt { true } else { false };
149149
| -------^^^^^^^------ help: try this: `if opt.is_some()`
150150

151151
error: redundant pattern matching, consider using `is_some()`
152-
--> $DIR/redundant_pattern_matching.rs:102:20
152+
--> $DIR/redundant_pattern_matching.rs:103:20
153153
|
154154
LL | let _ = if let Some(_) = gen_opt() {
155155
| -------^^^^^^^------------ help: try this: `if gen_opt().is_some()`
156156

157157
error: redundant pattern matching, consider using `is_none()`
158-
--> $DIR/redundant_pattern_matching.rs:104:19
158+
--> $DIR/redundant_pattern_matching.rs:105:19
159159
|
160160
LL | } else if let None = gen_opt() {
161161
| -------^^^^------------ help: try this: `if gen_opt().is_none()`
162162

163163
error: redundant pattern matching, consider using `is_ok()`
164-
--> $DIR/redundant_pattern_matching.rs:106:19
164+
--> $DIR/redundant_pattern_matching.rs:107:19
165165
|
166166
LL | } else if let Ok(_) = gen_res() {
167167
| -------^^^^^------------ help: try this: `if gen_res().is_ok()`
168168

169169
error: redundant pattern matching, consider using `is_err()`
170-
--> $DIR/redundant_pattern_matching.rs:108:19
170+
--> $DIR/redundant_pattern_matching.rs:109:19
171171
|
172172
LL | } else if let Err(_) = gen_res() {
173173
| -------^^^^^^------------ help: try this: `if gen_res().is_err()`
174174

175175
error: redundant pattern matching, consider using `is_some()`
176-
--> $DIR/redundant_pattern_matching.rs:141:19
176+
--> $DIR/redundant_pattern_matching.rs:142:19
177177
|
178178
LL | while let Some(_) = r#try!(result_opt()) {}
179179
| ----------^^^^^^^----------------------- help: try this: `while r#try!(result_opt()).is_some()`
180180

181181
error: redundant pattern matching, consider using `is_some()`
182-
--> $DIR/redundant_pattern_matching.rs:142:16
182+
--> $DIR/redundant_pattern_matching.rs:143:16
183183
|
184184
LL | if let Some(_) = r#try!(result_opt()) {}
185185
| -------^^^^^^^----------------------- help: try this: `if r#try!(result_opt()).is_some()`
186186

187187
error: redundant pattern matching, consider using `is_some()`
188-
--> $DIR/redundant_pattern_matching.rs:148:12
188+
--> $DIR/redundant_pattern_matching.rs:149:12
189189
|
190190
LL | if let Some(_) = m!() {}
191191
| -------^^^^^^^------- help: try this: `if m!().is_some()`
192192

193193
error: redundant pattern matching, consider using `is_some()`
194-
--> $DIR/redundant_pattern_matching.rs:149:15
194+
--> $DIR/redundant_pattern_matching.rs:150:15
195195
|
196196
LL | while let Some(_) = m!() {}
197197
| ----------^^^^^^^------- help: try this: `while m!().is_some()`

0 commit comments

Comments
 (0)