redundant_pattern: take binding (ref, ref mut) into account in suggestion.

fixes #5271
This commit is contained in:
Matthias Krüger 2020-03-08 10:00:23 +01:00
parent 3d0f0e33af
commit 75a2300e27
4 changed files with 55 additions and 5 deletions

View File

@ -5,8 +5,8 @@
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 @@ fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat) {
}
}
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 @@ fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat) {
ident.name, ident.name,
),
"try",
format!("{}", ident.name),
format!("{}{}", left_binding, ident.name),
Applicability::MachineApplicable,
);
}

View File

@ -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] => (),
}
}

View File

@ -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] => (),
}
}

View File

@ -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