Skip to content

Commit 3ddf960

Browse files
committed
Fix redundant_pattern false positive
1 parent a3fcaee commit 3ddf960

File tree

6 files changed

+48
-44
lines changed

6 files changed

+48
-44
lines changed

clippy_lints/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,6 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
813813
misc::CMP_OWNED,
814814
misc::FLOAT_CMP,
815815
misc::MODULO_ONE,
816-
misc::REDUNDANT_PATTERN,
817816
misc::SHORT_CIRCUIT_STATEMENT,
818817
misc::TOPLEVEL_REF_ARG,
819818
misc::ZERO_PTR,
@@ -822,6 +821,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
822821
misc_early::DUPLICATE_UNDERSCORE_ARGUMENT,
823822
misc_early::MIXED_CASE_HEX_LITERALS,
824823
misc_early::REDUNDANT_CLOSURE_CALL,
824+
misc_early::REDUNDANT_PATTERN,
825825
misc_early::UNNEEDED_FIELD_PATTERN,
826826
misc_early::ZERO_PREFIXED_LITERAL,
827827
mut_reference::UNNECESSARY_MUT_PASSED,
@@ -964,13 +964,13 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
964964
methods::STRING_EXTEND_CHARS,
965965
methods::UNNECESSARY_FOLD,
966966
methods::WRONG_SELF_CONVENTION,
967-
misc::REDUNDANT_PATTERN,
968967
misc::TOPLEVEL_REF_ARG,
969968
misc::ZERO_PTR,
970969
misc_early::BUILTIN_TYPE_SHADOW,
971970
misc_early::DOUBLE_NEG,
972971
misc_early::DUPLICATE_UNDERSCORE_ARGUMENT,
973972
misc_early::MIXED_CASE_HEX_LITERALS,
973+
misc_early::REDUNDANT_PATTERN,
974974
misc_early::UNNEEDED_FIELD_PATTERN,
975975
mut_reference::UNNECESSARY_MUT_PASSED,
976976
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: 38 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,20 @@ 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(
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+
);
324+
}
325+
}
289326
}
290327

291328
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
@@ -1545,7 +1545,7 @@ pub const ALL_LINTS: [Lint; 311] = [
15451545
group: "style",
15461546
desc: "using `name @ _` in a pattern",
15471547
deprecation: None,
1548-
module: "misc",
1548+
module: "misc_early",
15491549
},
15501550
Lint {
15511551
name: "redundant_pattern_matching",

tests/ui/patterns.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#![allow(unused)]
22
#![warn(clippy::all)]
3+
#![feature(slice_patterns)]
34

45
fn main() {
56
let v = Some(true);
7+
let s = [0, 1, 2, 3, 4];
68
match v {
79
Some(x) => (),
810
y @ _ => (),
@@ -11,4 +13,8 @@ fn main() {
1113
Some(x) => (),
1214
y @ None => (), // no error
1315
}
16+
match s {
17+
[x, inside @ .., y] => (), // no error
18+
[..] => (),
19+
}
1420
}

tests/ui/patterns.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: the `y @ _` pattern can be written as just `y`
2-
--> $DIR/patterns.rs:8:9
2+
--> $DIR/patterns.rs:10:9
33
|
44
LL | y @ _ => (),
55
| ^^^^^

0 commit comments

Comments
 (0)