From 0a6790779a4ca66de05cfd8e197f1a2a6066b085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Sun, 8 Mar 2020 10:00:23 +0100 Subject: [PATCH] redundant_pattern: take binding (ref, ref mut) into account in suggestion. fixes #5271 --- clippy_lints/src/misc_early.rs | 14 ++++++++++---- tests/ui/patterns.fixed | 16 ++++++++++++++++ tests/ui/patterns.rs | 16 ++++++++++++++++ tests/ui/patterns.stderr | 14 +++++++++++++- 4 files changed, 55 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/misc_early.rs b/clippy_lints/src/misc_early.rs index 60b672766231..28bcc3f841af 100644 --- a/clippy_lints/src/misc_early.rs +++ b/clippy_lints/src/misc_early.rs @@ -5,8 +5,8 @@ use crate::utils::{ use if_chain::if_chain; use rustc::lint::in_external_macro; use rustc_ast::ast::{ - Block, Expr, ExprKind, GenericParamKind, Generics, Lit, LitFloatType, LitIntType, LitKind, NodeId, Pat, PatKind, - StmtKind, UnOp, + BindingMode, Block, Expr, ExprKind, GenericParamKind, Generics, Lit, LitFloatType, LitIntType, LitKind, Mutability, + NodeId, Pat, PatKind, StmtKind, UnOp, }; use rustc_ast::visit::{walk_expr, FnKind, Visitor}; use rustc_data_structures::fx::FxHashMap; @@ -355,7 +355,13 @@ impl EarlyLintPass for MiscEarlyLints { } } - if let PatKind::Ident(_, ident, Some(ref right)) = pat.kind { + if let PatKind::Ident(left, ident, Some(ref right)) = pat.kind { + let left_binding = match left { + BindingMode::ByRef(Mutability::Mut) => "ref mut ", + BindingMode::ByRef(Mutability::Not) => "ref ", + _ => "", + }; + if let PatKind::Wild = right.kind { span_lint_and_sugg( cx, @@ -366,7 +372,7 @@ impl EarlyLintPass for MiscEarlyLints { ident.name, ident.name, ), "try", - format!("{}", ident.name), + format!("{}{}", left_binding, ident.name), Applicability::MachineApplicable, ); } diff --git a/tests/ui/patterns.fixed b/tests/ui/patterns.fixed index dfe27e193b97..67d0bdf3cfa8 100644 --- a/tests/ui/patterns.fixed +++ b/tests/ui/patterns.fixed @@ -17,4 +17,20 @@ fn main() { [x, inside @ .., y] => (), // no error [..] => (), } + + let mut mutv = vec![1, 2, 3]; + + // required "ref" left out in suggestion: #5271 + match mutv { + ref mut x => { + x.push(4); + println!("vec: {:?}", x); + }, + ref y if y == &vec![0] => (), + } + + match mutv { + ref x => println!("vec: {:?}", x), + ref y if y == &vec![0] => (), + } } diff --git a/tests/ui/patterns.rs b/tests/ui/patterns.rs index bd202fc04205..6a252be3c550 100644 --- a/tests/ui/patterns.rs +++ b/tests/ui/patterns.rs @@ -17,4 +17,20 @@ fn main() { [x, inside @ .., y] => (), // no error [..] => (), } + + let mut mutv = vec![1, 2, 3]; + + // required "ref" left out in suggestion: #5271 + match mutv { + ref mut x @ _ => { + x.push(4); + println!("vec: {:?}", x); + }, + ref y if y == &vec![0] => (), + } + + match mutv { + ref x @ _ => println!("vec: {:?}", x), + ref y if y == &vec![0] => (), + } } diff --git a/tests/ui/patterns.stderr b/tests/ui/patterns.stderr index f25e71e872b2..af067580688b 100644 --- a/tests/ui/patterns.stderr +++ b/tests/ui/patterns.stderr @@ -6,5 +6,17 @@ LL | y @ _ => (), | = note: `-D clippy::redundant-pattern` implied by `-D warnings` -error: aborting due to previous error +error: the `x @ _` pattern can be written as just `x` + --> $DIR/patterns.rs:25:9 + | +LL | ref mut x @ _ => { + | ^^^^^^^^^^^^^ help: try: `ref mut x` + +error: the `x @ _` pattern can be written as just `x` + --> $DIR/patterns.rs:33:9 + | +LL | ref x @ _ => println!("vec: {:?}", x), + | ^^^^^^^^^ help: try: `ref x` + +error: aborting due to 3 previous errors