Skip to content

Commit 91b8bed

Browse files
committed
cover guard patterns in clippy lints
1 parent 3356e7e commit 91b8bed

File tree

7 files changed

+22
-6
lines changed

7 files changed

+22
-6
lines changed

src/tools/clippy/clippy_lints/src/equatable_if_let.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn unary_pattern(pat: &Pat<'_>) -> bool {
5555
| PatKind::Err(_) => false,
5656
PatKind::Struct(_, a, etc) => !etc && a.iter().all(|x| unary_pattern(x.pat)),
5757
PatKind::Tuple(a, etc) | PatKind::TupleStruct(_, a, etc) => etc.as_opt_usize().is_none() && array_rec(a),
58-
PatKind::Ref(x, _) | PatKind::Box(x) | PatKind::Deref(x) => unary_pattern(x),
58+
PatKind::Ref(x, _) | PatKind::Box(x) | PatKind::Deref(x) | PatKind::Guard(x, _) => unary_pattern(x),
5959
PatKind::Path(_) | PatKind::Lit(_) => true,
6060
}
6161
}

src/tools/clippy/clippy_lints/src/matches/match_same_arms.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,11 @@ impl<'a> NormalizedPat<'a> {
258258
fn from_pat(cx: &LateContext<'_>, arena: &'a DroplessArena, pat: &'a Pat<'_>) -> Self {
259259
match pat.kind {
260260
PatKind::Wild | PatKind::Binding(.., None) => Self::Wild,
261-
PatKind::Binding(.., Some(pat)) | PatKind::Box(pat) | PatKind::Deref(pat) | PatKind::Ref(pat, _) => {
262-
Self::from_pat(cx, arena, pat)
263-
},
261+
PatKind::Binding(.., Some(pat))
262+
| PatKind::Box(pat)
263+
| PatKind::Deref(pat)
264+
| PatKind::Ref(pat, _)
265+
| PatKind::Guard(pat, _) => Self::from_pat(cx, arena, pat),
264266
PatKind::Never => Self::Never,
265267
PatKind::Struct(ref path, fields, _) => {
266268
let fields =

src/tools/clippy/clippy_lints/src/matches/single_match.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ impl<'a> PatState<'a> {
340340
matches!(self, Self::Wild)
341341
},
342342

343+
PatKind::Guard(..) => {
344+
matches!(self, Self::Wild)
345+
}
346+
343347
// Patterns for things which can only contain a single sub-pattern.
344348
PatKind::Binding(_, _, _, Some(pat)) | PatKind::Ref(pat, _) | PatKind::Box(pat) | PatKind::Deref(pat) => {
345349
self.add_pat(cx, pat)

src/tools/clippy/clippy_lints/src/unnested_or_patterns.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ fn transform_with_focus_on_idx(alternatives: &mut ThinVec<P<Pat>>, focus_idx: us
234234
// In the case of only two patterns, replacement adds net characters.
235235
| Ref(_, Mutability::Not)
236236
// Dealt with elsewhere.
237-
| Or(_) | Paren(_) | Deref(_) => false,
237+
| Or(_) | Paren(_) | Deref(_) | Guard(..) => false,
238238
// Transform `box x | ... | box y` into `box (x | y)`.
239239
//
240240
// The cases below until `Slice(...)` deal with *singleton* products.

src/tools/clippy/clippy_lints/src/utils/author.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,12 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
712712
kind!("Ref({pat}, Mutability::{muta:?})");
713713
self.pat(pat);
714714
},
715+
PatKind::Guard(pat, cond) => {
716+
bind!(self, pat, cond);
717+
kind!("Guard({pat}, {cond})");
718+
self.pat(pat);
719+
self.expr(cond);
720+
}
715721
PatKind::Lit(lit_expr) => {
716722
bind!(self, lit_expr);
717723
kind!("Lit({lit_expr})");

src/tools/clippy/clippy_utils/src/hir_utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,10 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
10881088
self.hash_pat(pat);
10891089
std::mem::discriminant(&mu).hash(&mut self.s);
10901090
},
1091+
PatKind::Guard(pat, guard) => {
1092+
self.hash_pat(pat);
1093+
self.hash_expr(guard);
1094+
},
10911095
PatKind::Slice(l, m, r) => {
10921096
for pat in l {
10931097
self.hash_pat(pat);

src/tools/clippy/clippy_utils/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,7 @@ pub fn is_refutable(cx: &LateContext<'_>, pat: &Pat<'_>) -> bool {
17801780
},
17811781
}
17821782
},
1783-
PatKind::Lit(..) | PatKind::Range(..) | PatKind::Err(_) => true,
1783+
PatKind::Lit(..) | PatKind::Range(..) | PatKind::Guard(..) | PatKind::Err(_) => true,
17841784
}
17851785
}
17861786

0 commit comments

Comments
 (0)