Skip to content

Commit bc0965e

Browse files
Implement macro-based deref!() syntax for deref patterns
Stop using `box PAT` syntax for deref patterns, as it's misleading and also causes their semantics being tangled up.
1 parent fb1b198 commit bc0965e

File tree

6 files changed

+11
-3
lines changed

6 files changed

+11
-3
lines changed

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) => unary_pattern(x),
58+
PatKind::Ref(x, _) | PatKind::Box(x) | PatKind::Deref(x) => unary_pattern(x),
5959
PatKind::Path(_) | PatKind::Lit(_) => true,
6060
}
6161
}

clippy_lints/src/matches/match_same_arms.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl<'a> NormalizedPat<'a> {
243243
fn from_pat(cx: &LateContext<'_>, arena: &'a DroplessArena, pat: &'a Pat<'_>) -> Self {
244244
match pat.kind {
245245
PatKind::Wild | PatKind::Binding(.., None) => Self::Wild,
246-
PatKind::Binding(.., Some(pat)) | PatKind::Box(pat) | PatKind::Ref(pat, _) => {
246+
PatKind::Binding(.., Some(pat)) | PatKind::Box(pat) | PatKind::Deref(pat) | PatKind::Ref(pat, _) => {
247247
Self::from_pat(cx, arena, pat)
248248
},
249249
PatKind::Never => Self::Never,

clippy_lints/src/unnested_or_patterns.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ fn transform_with_focus_on_idx(alternatives: &mut ThinVec<P<Pat>>, focus_idx: us
242242
|k| matches!(k, Box(_)),
243243
|k| always_pat!(k, Box(p) => p),
244244
),
245+
// FIXME(deref_patterns): Should we merge patterns here?
246+
Deref(_) => false,
245247
// Transform `&mut x | ... | &mut y` into `&mut (x | y)`.
246248
Ref(target, Mutability::Mut) => extend_with_matching(
247249
target, start, alternatives,

clippy_lints/src/utils/author.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,11 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
689689
kind!("Box({pat})");
690690
self.pat(pat);
691691
},
692+
PatKind::Deref(pat) => {
693+
bind!(self, pat);
694+
kind!("Deref({pat})");
695+
self.pat(pat);
696+
},
692697
PatKind::Ref(pat, muta) => {
693698
bind!(self, pat);
694699
kind!("Ref({pat}, Mutability::{muta:?})");

clippy_utils/src/hir_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
955955
}
956956
},
957957
PatKind::Box(pat) => self.hash_pat(pat),
958+
PatKind::Deref(pat) => self.hash_pat(pat),
958959
PatKind::Lit(expr) => self.hash_expr(expr),
959960
PatKind::Or(pats) => {
960961
for pat in pats {

clippy_utils/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1678,7 +1678,7 @@ pub fn is_refutable(cx: &LateContext<'_>, pat: &Pat<'_>) -> bool {
16781678
match pat.kind {
16791679
PatKind::Wild | PatKind::Never => false, // If `!` typechecked then the type is empty, so not refutable.
16801680
PatKind::Binding(_, _, _, pat) => pat.map_or(false, |pat| is_refutable(cx, pat)),
1681-
PatKind::Box(pat) | PatKind::Ref(pat, _) => is_refutable(cx, pat),
1681+
PatKind::Box(pat) | PatKind::Deref(pat) | PatKind::Ref(pat, _) => is_refutable(cx, pat),
16821682
PatKind::Path(ref qpath) => is_enum_variant(cx, qpath, pat.hir_id),
16831683
PatKind::Or(pats) => {
16841684
// TODO: should be the honest check, that pats is exhaustive set

0 commit comments

Comments
 (0)