Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions clippy_lints/src/misc_early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -366,7 +372,7 @@ impl EarlyLintPass for MiscEarlyLints {
ident.name, ident.name,
),
"try",
format!("{}", ident.name),
format!("{}{}", left_binding, ident.name),
Applicability::MachineApplicable,
);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/patterns.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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] => (),
}
}
16 changes: 16 additions & 0 deletions tests/ui/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] => (),
}
}
14 changes: 13 additions & 1 deletion tests/ui/patterns.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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