Skip to content

Commit 42f074d

Browse files
committed
Implement pattern matching for &pin mut|const T
1 parent 78db66e commit 42f074d

File tree

10 files changed

+23
-14
lines changed

10 files changed

+23
-14
lines changed

clippy_lints/src/index_refutable_slice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn find_slice_values(cx: &LateContext<'_>, pat: &hir::Pat<'_>) -> FxIndexMap<Hir
9494
// We'll just ignore mut and ref mut for simplicity sake right now
9595
if let hir::PatKind::Binding(hir::BindingMode(by_ref, hir::Mutability::Not), value_hir_id, ident, sub_pat) =
9696
pat.kind
97-
&& by_ref != hir::ByRef::Yes(hir::Mutability::Mut)
97+
&& !matches!(by_ref, hir::ByRef::Yes(_, hir::Mutability::Mut))
9898
{
9999
// This block catches bindings with sub patterns. It would be hard to build a correct suggestion
100100
// for them and it's likely that the user knows what they are doing in such a case.

clippy_lints/src/matches/match_as_ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ fn is_ref_some_arm(cx: &LateContext<'_>, arm: &Arm<'_>) -> Option<Mutability> {
6363
.qpath_res(qpath, arm.pat.hir_id)
6464
.ctor_parent(cx)
6565
.is_lang_item(cx, LangItem::OptionSome)
66-
&& let PatKind::Binding(BindingMode(ByRef::Yes(mutabl), _), .., ident, _) = first_pat.kind
66+
&& let PatKind::Binding(BindingMode(ByRef::Yes(_, mutabl), _), .., ident, _) = first_pat.kind
6767
&& let ExprKind::Call(e, [arg]) = peel_blocks(arm.body).kind
6868
&& e.res(cx).ctor_parent(cx).is_lang_item(cx, LangItem::OptionSome)
6969
&& let ExprKind::Path(QPath::Resolved(_, path2)) = arg.kind

clippy_lints/src/matches/needless_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ fn pat_same_as_expr(pat: &Pat<'_>, expr: &Expr<'_>) -> bool {
172172
},
173173
)),
174174
) => {
175-
return !matches!(annot, BindingMode(ByRef::Yes(_), _)) && pat_ident.name == first_seg.ident.name;
175+
return !matches!(annot, BindingMode(ByRef::Yes(..), _)) && pat_ident.name == first_seg.ident.name;
176176
},
177177
// Example: `Custom::TypeA => Custom::TypeB`, or `None => None`
178178
(

clippy_lints/src/matches/redundant_guards.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ fn get_pat_binding<'tcx>(
176176
if let PatKind::Binding(bind_annot, hir_id, ident, _) = pat.kind
177177
&& hir_id == local
178178
{
179-
if matches!(bind_annot.0, rustc_ast::ByRef::Yes(_)) {
179+
if matches!(bind_annot.0, rustc_ast::ByRef::Yes(..)) {
180180
let _ = byref_ident.insert(ident);
181181
}
182182
// the second call of `replace()` returns a `Some(span)`, meaning a multi-binding pattern

clippy_lints/src/methods/clone_on_copy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>)
5656
_ => false,
5757
},
5858
// local binding capturing a reference
59-
Node::LetStmt(l) if matches!(l.pat.kind, PatKind::Binding(BindingMode(ByRef::Yes(_), _), ..)) => {
59+
Node::LetStmt(l) if matches!(l.pat.kind, PatKind::Binding(BindingMode(ByRef::Yes(..), _), ..)) => {
6060
return;
6161
},
6262
_ => false,

clippy_lints/src/question_mark.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ fn check_let_some_else_return_none(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
150150
let init_expr_str = Sugg::hir_with_applicability(cx, init_expr, "..", &mut applicability).maybe_paren();
151151
// Take care when binding is `ref`
152152
let sugg = if let PatKind::Binding(
153-
BindingMode(ByRef::Yes(ref_mutability), binding_mutability),
153+
BindingMode(ByRef::Yes(_,ref_mutability), binding_mutability),
154154
_hir_id,
155155
ident,
156156
subpattern,
@@ -169,7 +169,7 @@ fn check_let_some_else_return_none(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
169169
// Handle subpattern (@ subpattern)
170170
let maybe_subpattern = match subpattern {
171171
Some(Pat {
172-
kind: PatKind::Binding(BindingMode(ByRef::Yes(_), _), _, subident, None),
172+
kind: PatKind::Binding(BindingMode(ByRef::Yes(..), _), _, subident, None),
173173
..
174174
}) => {
175175
// avoid `&ref`
@@ -504,8 +504,8 @@ fn check_if_let_some_or_err_and_early_return<'tcx>(cx: &LateContext<'tcx>, expr:
504504
let receiver_str = snippet_with_applicability(cx, let_expr.span, "..", &mut applicability);
505505
let requires_semi = matches!(cx.tcx.parent_hir_node(expr.hir_id), Node::Stmt(_));
506506
let method_call_str = match by_ref {
507-
ByRef::Yes(Mutability::Mut) => ".as_mut()",
508-
ByRef::Yes(Mutability::Not) => ".as_ref()",
507+
ByRef::Yes(_, Mutability::Mut) => ".as_mut()",
508+
ByRef::Yes(_, Mutability::Not) => ".as_ref()",
509509
ByRef::No => "",
510510
};
511511
let sugg = format!(

clippy_lints/src/toplevel_ref_arg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<'tcx> LateLintPass<'tcx> for ToplevelRefArg {
6161
) {
6262
if !matches!(k, FnKind::Closure) {
6363
for arg in iter_input_pats(decl, body) {
64-
if let PatKind::Binding(BindingMode(ByRef::Yes(_), _), ..) = arg.pat.kind
64+
if let PatKind::Binding(BindingMode(ByRef::Yes(..), _), ..) = arg.pat.kind
6565
&& is_lint_allowed(cx, REF_PATTERNS, arg.pat.hir_id)
6666
&& !arg.span.in_external_macro(cx.tcx.sess.source_map())
6767
{
@@ -80,7 +80,7 @@ impl<'tcx> LateLintPass<'tcx> for ToplevelRefArg {
8080

8181
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
8282
if let StmtKind::Let(local) = stmt.kind
83-
&& let PatKind::Binding(BindingMode(ByRef::Yes(mutabl), _), .., name, None) = local.pat.kind
83+
&& let PatKind::Binding(BindingMode(ByRef::Yes(_, mutabl), _), .., name, None) = local.pat.kind
8484
&& let Some(init) = local.init
8585
// Do not emit if clippy::ref_patterns is not allowed to avoid having two lints for the same issue.
8686
&& is_lint_allowed(cx, REF_PATTERNS, local.pat.hir_id)

clippy_lints/src/utils/author.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,10 +745,14 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
745745
let ann = match ann {
746746
BindingMode::NONE => "NONE",
747747
BindingMode::REF => "REF",
748+
BindingMode::REF_PIN => "REF_PIN",
748749
BindingMode::MUT => "MUT",
749750
BindingMode::REF_MUT => "REF_MUT",
751+
BindingMode::REF_PIN_MUT => "REF_PIN_MUT",
750752
BindingMode::MUT_REF => "MUT_REF",
753+
BindingMode::MUT_REF_PIN => "MUT_REF_PIN",
751754
BindingMode::MUT_REF_MUT => "MUT_REF_MUT",
755+
BindingMode::MUT_REF_PIN_MUT => "MUT_REF_PIN_MUT",
752756
};
753757
kind!("Binding(BindingMode::{ann}, _, {name}, {sub})");
754758
self.ident(name);

clippy_utils/src/eager_or_lazy.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,12 @@ fn expr_eagerness<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> EagernessS
212212

213213
// Custom `Deref` impl might have side effects
214214
ExprKind::Unary(UnOp::Deref, e)
215-
if self.cx.typeck_results().expr_ty(e).builtin_deref(true).is_none() =>
215+
if self
216+
.cx
217+
.typeck_results()
218+
.expr_ty(e)
219+
.builtin_deref(true)
220+
.is_none() =>
216221
{
217222
self.eagerness |= NoChange;
218223
},

clippy_utils/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ pub fn capture_local_usage(cx: &LateContext<'_>, e: &Expr<'_>) -> CaptureKind {
783783
ByRef::No if !is_copy(cx, cx.typeck_results().node_type(id)) => {
784784
capture = CaptureKind::Value;
785785
},
786-
ByRef::Yes(Mutability::Mut) if capture != CaptureKind::Value => {
786+
ByRef::Yes(_, Mutability::Mut) if capture != CaptureKind::Value => {
787787
capture = CaptureKind::Ref(Mutability::Mut);
788788
},
789789
_ => (),
@@ -1831,7 +1831,7 @@ pub fn is_expr_identity_of_pat(cx: &LateContext<'_>, pat: &Pat<'_>, expr: &Expr<
18311831
.typeck_results()
18321832
.pat_binding_modes()
18331833
.get(pat.hir_id)
1834-
.is_some_and(|mode| matches!(mode.0, ByRef::Yes(_)))
1834+
.is_some_and(|mode| matches!(mode.0, ByRef::Yes(..)))
18351835
{
18361836
// If the parameter is `(x, y)` of type `&(T, T)`, or `[x, y]` of type `&[T; 2]`, then
18371837
// due to match ergonomics, the inner patterns become references. Don't consider this

0 commit comments

Comments
 (0)