Skip to content

Commit 790fb93

Browse files
committed
check for matches! macro directly in redundant_guards
1 parent 272413f commit 790fb93

File tree

4 files changed

+48
-34
lines changed

4 files changed

+48
-34
lines changed

clippy_lints/src/matches/redundant_guards.rs

+7-17
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
use clippy_config::msrvs::Msrv;
22
use clippy_utils::diagnostics::span_lint_and_then;
3+
use clippy_utils::macros::matching_root_macro_call;
34
use clippy_utils::source::snippet;
45
use clippy_utils::visitors::{for_each_expr, is_local_used};
56
use clippy_utils::{in_constant, path_to_local};
67
use rustc_ast::{BorrowKind, LitKind};
78
use rustc_errors::Applicability;
89
use rustc_hir::def::{DefKind, Res};
9-
use rustc_hir::{Arm, BinOpKind, Expr, ExprKind, MatchSource, Node, Pat, PatKind, UnOp};
10+
use rustc_hir::{Arm, BinOpKind, Expr, ExprKind, MatchSource, Node, PatKind, UnOp};
1011
use rustc_lint::LateContext;
1112
use rustc_span::symbol::Ident;
12-
use rustc_span::{Span, Symbol};
13+
use rustc_span::{sym, Span, Symbol};
1314
use std::borrow::Cow;
1415
use std::ops::ControlFlow;
1516

@@ -22,21 +23,10 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'tcx>], msrv:
2223
};
2324

2425
// `Some(x) if matches!(x, y)`
25-
if let ExprKind::Match(
26-
scrutinee,
27-
[
28-
arm,
29-
Arm {
30-
pat: Pat {
31-
kind: PatKind::Wild, ..
32-
},
33-
..
34-
},
35-
],
36-
MatchSource::Normal,
37-
) = guard.kind
26+
if let ExprKind::Match(scrutinee, [arm, _], MatchSource::Normal) = guard.kind
27+
&& matching_root_macro_call(cx, guard.span, sym::matches_macro).is_some()
3828
&& let Some(binding) = get_pat_binding(cx, scrutinee, outer_arm)
39-
&& !pat_contains_disallowed_or(&arm.pat, msrv)
29+
&& !pat_contains_disallowed_or(arm.pat, msrv)
4030
{
4131
let pat_span = match (arm.pat.kind, binding.byref_ident) {
4232
(PatKind::Ref(pat, _), Some(_)) => pat.span,
@@ -55,7 +45,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'tcx>], msrv:
5545
// `Some(x) if let Some(2) = x`
5646
else if let ExprKind::Let(let_expr) = guard.kind
5747
&& let Some(binding) = get_pat_binding(cx, let_expr.init, outer_arm)
58-
&& !pat_contains_disallowed_or(&let_expr.pat, msrv)
48+
&& !pat_contains_disallowed_or(let_expr.pat, msrv)
5949
{
6050
let pat_span = match (let_expr.pat.kind, binding.byref_ident) {
6151
(PatKind::Ref(pat, _), Some(_)) => pat.span,

tests/ui/redundant_guards.fixed

+12
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,18 @@ fn f(s: Option<std::ffi::OsString>) {
136136
}
137137
}
138138

139+
fn not_matches() {
140+
match Some(42) {
141+
// The pattern + guard is not equivalent to `Some(42)` because of the `panic!`
142+
Some(v)
143+
if match v {
144+
42 => true,
145+
_ => panic!(),
146+
} => {},
147+
_ => {},
148+
}
149+
}
150+
139151
struct S {
140152
a: usize,
141153
}

tests/ui/redundant_guards.rs

+12
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,18 @@ fn f(s: Option<std::ffi::OsString>) {
136136
}
137137
}
138138

139+
fn not_matches() {
140+
match Some(42) {
141+
// The pattern + guard is not equivalent to `Some(42)` because of the `panic!`
142+
Some(v)
143+
if match v {
144+
42 => true,
145+
_ => panic!(),
146+
} => {},
147+
_ => {},
148+
}
149+
}
150+
139151
struct S {
140152
a: usize,
141153
}

tests/ui/redundant_guards.stderr

+17-17
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ LL + 1 => {},
132132
|
133133

134134
error: redundant guard
135-
--> tests/ui/redundant_guards.rs:174:28
135+
--> tests/ui/redundant_guards.rs:186:28
136136
|
137137
LL | Some(ref x) if x == &1 => {},
138138
| ^^^^^^^
@@ -144,7 +144,7 @@ LL + Some(1) => {},
144144
|
145145

146146
error: redundant guard
147-
--> tests/ui/redundant_guards.rs:175:28
147+
--> tests/ui/redundant_guards.rs:187:28
148148
|
149149
LL | Some(ref x) if &1 == x => {},
150150
| ^^^^^^^
@@ -156,7 +156,7 @@ LL + Some(1) => {},
156156
|
157157

158158
error: redundant guard
159-
--> tests/ui/redundant_guards.rs:176:28
159+
--> tests/ui/redundant_guards.rs:188:28
160160
|
161161
LL | Some(ref x) if let &2 = x => {},
162162
| ^^^^^^^^^^
@@ -168,7 +168,7 @@ LL + Some(2) => {},
168168
|
169169

170170
error: redundant guard
171-
--> tests/ui/redundant_guards.rs:177:28
171+
--> tests/ui/redundant_guards.rs:189:28
172172
|
173173
LL | Some(ref x) if matches!(x, &3) => {},
174174
| ^^^^^^^^^^^^^^^
@@ -180,7 +180,7 @@ LL + Some(3) => {},
180180
|
181181

182182
error: redundant guard
183-
--> tests/ui/redundant_guards.rs:197:32
183+
--> tests/ui/redundant_guards.rs:209:32
184184
|
185185
LL | B { ref c, .. } if c == &1 => {},
186186
| ^^^^^^^
@@ -192,7 +192,7 @@ LL + B { c: 1, .. } => {},
192192
|
193193

194194
error: redundant guard
195-
--> tests/ui/redundant_guards.rs:198:32
195+
--> tests/ui/redundant_guards.rs:210:32
196196
|
197197
LL | B { ref c, .. } if &1 == c => {},
198198
| ^^^^^^^
@@ -204,7 +204,7 @@ LL + B { c: 1, .. } => {},
204204
|
205205

206206
error: redundant guard
207-
--> tests/ui/redundant_guards.rs:199:32
207+
--> tests/ui/redundant_guards.rs:211:32
208208
|
209209
LL | B { ref c, .. } if let &1 = c => {},
210210
| ^^^^^^^^^^
@@ -216,7 +216,7 @@ LL + B { c: 1, .. } => {},
216216
|
217217

218218
error: redundant guard
219-
--> tests/ui/redundant_guards.rs:200:32
219+
--> tests/ui/redundant_guards.rs:212:32
220220
|
221221
LL | B { ref c, .. } if matches!(c, &1) => {},
222222
| ^^^^^^^^^^^^^^^
@@ -228,7 +228,7 @@ LL + B { c: 1, .. } => {},
228228
|
229229

230230
error: redundant guard
231-
--> tests/ui/redundant_guards.rs:210:26
231+
--> tests/ui/redundant_guards.rs:222:26
232232
|
233233
LL | Some(Some(x)) if x.is_empty() => {},
234234
| ^^^^^^^^^^^^
@@ -240,7 +240,7 @@ LL + Some(Some("")) => {},
240240
|
241241

242242
error: redundant guard
243-
--> tests/ui/redundant_guards.rs:221:26
243+
--> tests/ui/redundant_guards.rs:233:26
244244
|
245245
LL | Some(Some(x)) if x.is_empty() => {},
246246
| ^^^^^^^^^^^^
@@ -252,7 +252,7 @@ LL + Some(Some([])) => {},
252252
|
253253

254254
error: redundant guard
255-
--> tests/ui/redundant_guards.rs:226:26
255+
--> tests/ui/redundant_guards.rs:238:26
256256
|
257257
LL | Some(Some(x)) if x.is_empty() => {},
258258
| ^^^^^^^^^^^^
@@ -264,7 +264,7 @@ LL + Some(Some([])) => {},
264264
|
265265

266266
error: redundant guard
267-
--> tests/ui/redundant_guards.rs:237:26
267+
--> tests/ui/redundant_guards.rs:249:26
268268
|
269269
LL | Some(Some(x)) if x.starts_with(&[]) => {},
270270
| ^^^^^^^^^^^^^^^^^^
@@ -276,7 +276,7 @@ LL + Some(Some([..])) => {},
276276
|
277277

278278
error: redundant guard
279-
--> tests/ui/redundant_guards.rs:242:26
279+
--> tests/ui/redundant_guards.rs:254:26
280280
|
281281
LL | Some(Some(x)) if x.starts_with(&[1]) => {},
282282
| ^^^^^^^^^^^^^^^^^^^
@@ -288,7 +288,7 @@ LL + Some(Some([1, ..])) => {},
288288
|
289289

290290
error: redundant guard
291-
--> tests/ui/redundant_guards.rs:247:26
291+
--> tests/ui/redundant_guards.rs:259:26
292292
|
293293
LL | Some(Some(x)) if x.starts_with(&[1, 2]) => {},
294294
| ^^^^^^^^^^^^^^^^^^^^^^
@@ -300,7 +300,7 @@ LL + Some(Some([1, 2, ..])) => {},
300300
|
301301

302302
error: redundant guard
303-
--> tests/ui/redundant_guards.rs:252:26
303+
--> tests/ui/redundant_guards.rs:264:26
304304
|
305305
LL | Some(Some(x)) if x.ends_with(&[1, 2]) => {},
306306
| ^^^^^^^^^^^^^^^^^^^^
@@ -312,7 +312,7 @@ LL + Some(Some([.., 1, 2])) => {},
312312
|
313313

314314
error: redundant guard
315-
--> tests/ui/redundant_guards.rs:274:18
315+
--> tests/ui/redundant_guards.rs:286:18
316316
|
317317
LL | y if y.is_empty() => {},
318318
| ^^^^^^^^^^^^
@@ -324,7 +324,7 @@ LL + "" => {},
324324
|
325325

326326
error: redundant guard
327-
--> tests/ui/redundant_guards.rs:293:22
327+
--> tests/ui/redundant_guards.rs:305:22
328328
|
329329
LL | y if y.is_empty() => {},
330330
| ^^^^^^^^^^^^

0 commit comments

Comments
 (0)