Skip to content

Commit d8f64b6

Browse files
committed
Auto merge of #5287 - matthiaskrgr:pat_isref, r=flip1995
redundant_pattern: take binding (ref, ref mut) into account in suggestion fixes #5271 changelog: redundant_pattern: take binding (ref, ref mut) into account in suggestion (#5271)
2 parents 92e25bb + 75a2300 commit d8f64b6

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

clippy_lints/src/misc_early.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use crate::utils::{
55
use if_chain::if_chain;
66
use rustc::lint::in_external_macro;
77
use rustc_ast::ast::{
8-
Block, Expr, ExprKind, GenericParamKind, Generics, Lit, LitFloatType, LitIntType, LitKind, NodeId, Pat, PatKind,
9-
StmtKind, UnOp,
8+
BindingMode, Block, Expr, ExprKind, GenericParamKind, Generics, Lit, LitFloatType, LitIntType, LitKind, Mutability,
9+
NodeId, Pat, PatKind, StmtKind, UnOp,
1010
};
1111
use rustc_ast::visit::{walk_expr, FnKind, Visitor};
1212
use rustc_data_structures::fx::FxHashMap;
@@ -356,7 +356,13 @@ impl EarlyLintPass for MiscEarlyLints {
356356
}
357357
}
358358

359-
if let PatKind::Ident(_, ident, Some(ref right)) = pat.kind {
359+
if let PatKind::Ident(left, ident, Some(ref right)) = pat.kind {
360+
let left_binding = match left {
361+
BindingMode::ByRef(Mutability::Mut) => "ref mut ",
362+
BindingMode::ByRef(Mutability::Not) => "ref ",
363+
_ => "",
364+
};
365+
360366
if let PatKind::Wild = right.kind {
361367
span_lint_and_sugg(
362368
cx,
@@ -367,7 +373,7 @@ impl EarlyLintPass for MiscEarlyLints {
367373
ident.name, ident.name,
368374
),
369375
"try",
370-
format!("{}", ident.name),
376+
format!("{}{}", left_binding, ident.name),
371377
Applicability::MachineApplicable,
372378
);
373379
}

tests/ui/patterns.fixed

+16
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,20 @@ fn main() {
1717
[x, inside @ .., y] => (), // no error
1818
[..] => (),
1919
}
20+
21+
let mut mutv = vec![1, 2, 3];
22+
23+
// required "ref" left out in suggestion: #5271
24+
match mutv {
25+
ref mut x => {
26+
x.push(4);
27+
println!("vec: {:?}", x);
28+
},
29+
ref y if y == &vec![0] => (),
30+
}
31+
32+
match mutv {
33+
ref x => println!("vec: {:?}", x),
34+
ref y if y == &vec![0] => (),
35+
}
2036
}

tests/ui/patterns.rs

+16
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,20 @@ fn main() {
1717
[x, inside @ .., y] => (), // no error
1818
[..] => (),
1919
}
20+
21+
let mut mutv = vec![1, 2, 3];
22+
23+
// required "ref" left out in suggestion: #5271
24+
match mutv {
25+
ref mut x @ _ => {
26+
x.push(4);
27+
println!("vec: {:?}", x);
28+
},
29+
ref y if y == &vec![0] => (),
30+
}
31+
32+
match mutv {
33+
ref x @ _ => println!("vec: {:?}", x),
34+
ref y if y == &vec![0] => (),
35+
}
2036
}

tests/ui/patterns.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,17 @@ LL | y @ _ => (),
66
|
77
= note: `-D clippy::redundant-pattern` implied by `-D warnings`
88

9-
error: aborting due to previous error
9+
error: the `x @ _` pattern can be written as just `x`
10+
--> $DIR/patterns.rs:25:9
11+
|
12+
LL | ref mut x @ _ => {
13+
| ^^^^^^^^^^^^^ help: try: `ref mut x`
14+
15+
error: the `x @ _` pattern can be written as just `x`
16+
--> $DIR/patterns.rs:33:9
17+
|
18+
LL | ref x @ _ => println!("vec: {:?}", x),
19+
| ^^^^^^^^^ help: try: `ref x`
20+
21+
error: aborting due to 3 previous errors
1022

0 commit comments

Comments
 (0)