Skip to content

Commit 313b41f

Browse files
committed
Auto merge of #4489 - JohnTitor:fix-redundant-pattern-false-positive, r=flip1995
Fix `redundant_pattern` false positive Fix #4428 changelog: Fix `redundant_pattern` false positive
2 parents 5f28fda + 0b3f452 commit 313b41f

File tree

7 files changed

+74
-45
lines changed

7 files changed

+74
-45
lines changed

clippy_lints/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,6 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
815815
misc::CMP_OWNED,
816816
misc::FLOAT_CMP,
817817
misc::MODULO_ONE,
818-
misc::REDUNDANT_PATTERN,
819818
misc::SHORT_CIRCUIT_STATEMENT,
820819
misc::TOPLEVEL_REF_ARG,
821820
misc::ZERO_PTR,
@@ -824,6 +823,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
824823
misc_early::DUPLICATE_UNDERSCORE_ARGUMENT,
825824
misc_early::MIXED_CASE_HEX_LITERALS,
826825
misc_early::REDUNDANT_CLOSURE_CALL,
826+
misc_early::REDUNDANT_PATTERN,
827827
misc_early::UNNEEDED_FIELD_PATTERN,
828828
misc_early::ZERO_PREFIXED_LITERAL,
829829
mut_reference::UNNECESSARY_MUT_PASSED,
@@ -967,13 +967,13 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
967967
methods::STRING_EXTEND_CHARS,
968968
methods::UNNECESSARY_FOLD,
969969
methods::WRONG_SELF_CONVENTION,
970-
misc::REDUNDANT_PATTERN,
971970
misc::TOPLEVEL_REF_ARG,
972971
misc::ZERO_PTR,
973972
misc_early::BUILTIN_TYPE_SHADOW,
974973
misc_early::DOUBLE_NEG,
975974
misc_early::DUPLICATE_UNDERSCORE_ARGUMENT,
976975
misc_early::MIXED_CASE_HEX_LITERALS,
976+
misc_early::REDUNDANT_PATTERN,
977977
misc_early::UNNEEDED_FIELD_PATTERN,
978978
mut_reference::UNNECESSARY_MUT_PASSED,
979979
neg_multiply::NEG_MULTIPLY,

clippy_lints/src/misc.rs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -136,28 +136,6 @@ declare_clippy_lint! {
136136
"taking a number modulo 1, which always returns 0"
137137
}
138138

139-
declare_clippy_lint! {
140-
/// **What it does:** Checks for patterns in the form `name @ _`.
141-
///
142-
/// **Why is this bad?** It's almost always more readable to just use direct
143-
/// bindings.
144-
///
145-
/// **Known problems:** None.
146-
///
147-
/// **Example:**
148-
/// ```rust
149-
/// # let v = Some("abc");
150-
///
151-
/// match v {
152-
/// Some(x) => (),
153-
/// y @ _ => (), // easier written as `y`,
154-
/// }
155-
/// ```
156-
pub REDUNDANT_PATTERN,
157-
style,
158-
"using `name @ _` in a pattern"
159-
}
160-
161139
declare_clippy_lint! {
162140
/// **What it does:** Checks for the use of bindings with a single leading
163141
/// underscore.
@@ -247,7 +225,6 @@ declare_lint_pass!(MiscLints => [
247225
FLOAT_CMP,
248226
CMP_OWNED,
249227
MODULO_ONE,
250-
REDUNDANT_PATTERN,
251228
USED_UNDERSCORE_BINDING,
252229
SHORT_CIRCUIT_STATEMENT,
253230
ZERO_PTR,
@@ -459,22 +436,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
459436
);
460437
}
461438
}
462-
463-
fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) {
464-
if let PatKind::Binding(.., ident, Some(ref right)) = pat.node {
465-
if let PatKind::Wild = right.node {
466-
span_lint(
467-
cx,
468-
REDUNDANT_PATTERN,
469-
pat.span,
470-
&format!(
471-
"the `{} @ _` pattern can be written as just `{}`",
472-
ident.name, ident.name
473-
),
474-
);
475-
}
476-
}
477-
}
478439
}
479440

480441
fn check_nan(cx: &LateContext<'_, '_>, path: &Path, expr: &Expr) {

clippy_lints/src/misc_early.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,28 @@ declare_clippy_lint! {
173173
"shadowing a builtin type"
174174
}
175175

176+
declare_clippy_lint! {
177+
/// **What it does:** Checks for patterns in the form `name @ _`.
178+
///
179+
/// **Why is this bad?** It's almost always more readable to just use direct
180+
/// bindings.
181+
///
182+
/// **Known problems:** None.
183+
///
184+
/// **Example:**
185+
/// ```rust
186+
/// # let v = Some("abc");
187+
///
188+
/// match v {
189+
/// Some(x) => (),
190+
/// y @ _ => (), // easier written as `y`,
191+
/// }
192+
/// ```
193+
pub REDUNDANT_PATTERN,
194+
style,
195+
"using `name @ _` in a pattern"
196+
}
197+
176198
declare_lint_pass!(MiscEarlyLints => [
177199
UNNEEDED_FIELD_PATTERN,
178200
DUPLICATE_UNDERSCORE_ARGUMENT,
@@ -181,7 +203,8 @@ declare_lint_pass!(MiscEarlyLints => [
181203
MIXED_CASE_HEX_LITERALS,
182204
UNSEPARATED_LITERAL_SUFFIX,
183205
ZERO_PREFIXED_LITERAL,
184-
BUILTIN_TYPE_SHADOW
206+
BUILTIN_TYPE_SHADOW,
207+
REDUNDANT_PATTERN
185208
]);
186209

187210
// Used to find `return` statements or equivalents e.g., `?`
@@ -286,6 +309,23 @@ impl EarlyLintPass for MiscEarlyLints {
286309
}
287310
}
288311
}
312+
313+
if let PatKind::Ident(_, ident, Some(ref right)) = pat.node {
314+
if let PatKind::Wild = right.node {
315+
span_lint_and_sugg(
316+
cx,
317+
REDUNDANT_PATTERN,
318+
pat.span,
319+
&format!(
320+
"the `{} @ _` pattern can be written as just `{}`",
321+
ident.name, ident.name,
322+
),
323+
"try",
324+
format!("{}", ident.name),
325+
Applicability::MachineApplicable,
326+
);
327+
}
328+
}
289329
}
290330

291331
fn check_fn(&mut self, cx: &EarlyContext<'_>, _: FnKind<'_>, decl: &FnDecl, _: Span, _: NodeId) {

src/lintlist/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1552,7 +1552,7 @@ pub const ALL_LINTS: [Lint; 313] = [
15521552
group: "style",
15531553
desc: "using `name @ _` in a pattern",
15541554
deprecation: None,
1555-
module: "misc",
1555+
module: "misc_early",
15561556
},
15571557
Lint {
15581558
name: "redundant_pattern_matching",

tests/ui/patterns.fixed

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// run-rustfix
2+
#![allow(unused)]
3+
#![warn(clippy::all)]
4+
#![feature(slice_patterns)]
5+
6+
fn main() {
7+
let v = Some(true);
8+
let s = [0, 1, 2, 3, 4];
9+
match v {
10+
Some(x) => (),
11+
y => (),
12+
}
13+
match v {
14+
Some(x) => (),
15+
y @ None => (), // no error
16+
}
17+
match s {
18+
[x, inside @ .., y] => (), // no error
19+
[..] => (),
20+
}
21+
}

tests/ui/patterns.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
// run-rustfix
12
#![allow(unused)]
23
#![warn(clippy::all)]
4+
#![feature(slice_patterns)]
35

46
fn main() {
57
let v = Some(true);
8+
let s = [0, 1, 2, 3, 4];
69
match v {
710
Some(x) => (),
811
y @ _ => (),
@@ -11,4 +14,8 @@ fn main() {
1114
Some(x) => (),
1215
y @ None => (), // no error
1316
}
17+
match s {
18+
[x, inside @ .., y] => (), // no error
19+
[..] => (),
20+
}
1421
}

tests/ui/patterns.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: the `y @ _` pattern can be written as just `y`
2-
--> $DIR/patterns.rs:8:9
2+
--> $DIR/patterns.rs:11:9
33
|
44
LL | y @ _ => (),
5-
| ^^^^^
5+
| ^^^^^ help: try: `y`
66
|
77
= note: `-D clippy::redundant-pattern` implied by `-D warnings`
88

0 commit comments

Comments
 (0)