From e236740f28745cfc24895426197c02fbcedd7c0b Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Tue, 3 Sep 2019 04:49:14 +0900 Subject: [PATCH 1/3] Fix `redundant_pattern` false positive --- clippy_lints/src/lib.rs | 4 ++-- clippy_lints/src/misc.rs | 39 ---------------------------------- clippy_lints/src/misc_early.rs | 39 +++++++++++++++++++++++++++++++++- src/lintlist/mod.rs | 2 +- tests/ui/patterns.rs | 6 ++++++ tests/ui/patterns.stderr | 2 +- 6 files changed, 48 insertions(+), 44 deletions(-) diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index a9294da59b6a..9d0a91c53182 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -815,7 +815,6 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con misc::CMP_OWNED, misc::FLOAT_CMP, misc::MODULO_ONE, - misc::REDUNDANT_PATTERN, misc::SHORT_CIRCUIT_STATEMENT, misc::TOPLEVEL_REF_ARG, misc::ZERO_PTR, @@ -824,6 +823,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con misc_early::DUPLICATE_UNDERSCORE_ARGUMENT, misc_early::MIXED_CASE_HEX_LITERALS, misc_early::REDUNDANT_CLOSURE_CALL, + misc_early::REDUNDANT_PATTERN, misc_early::UNNEEDED_FIELD_PATTERN, misc_early::ZERO_PREFIXED_LITERAL, mut_reference::UNNECESSARY_MUT_PASSED, @@ -967,13 +967,13 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con methods::STRING_EXTEND_CHARS, methods::UNNECESSARY_FOLD, methods::WRONG_SELF_CONVENTION, - misc::REDUNDANT_PATTERN, misc::TOPLEVEL_REF_ARG, misc::ZERO_PTR, misc_early::BUILTIN_TYPE_SHADOW, misc_early::DOUBLE_NEG, misc_early::DUPLICATE_UNDERSCORE_ARGUMENT, misc_early::MIXED_CASE_HEX_LITERALS, + misc_early::REDUNDANT_PATTERN, misc_early::UNNEEDED_FIELD_PATTERN, mut_reference::UNNECESSARY_MUT_PASSED, neg_multiply::NEG_MULTIPLY, diff --git a/clippy_lints/src/misc.rs b/clippy_lints/src/misc.rs index c6bcb845c7ad..a761f80b7939 100644 --- a/clippy_lints/src/misc.rs +++ b/clippy_lints/src/misc.rs @@ -136,28 +136,6 @@ declare_clippy_lint! { "taking a number modulo 1, which always returns 0" } -declare_clippy_lint! { - /// **What it does:** Checks for patterns in the form `name @ _`. - /// - /// **Why is this bad?** It's almost always more readable to just use direct - /// bindings. - /// - /// **Known problems:** None. - /// - /// **Example:** - /// ```rust - /// # let v = Some("abc"); - /// - /// match v { - /// Some(x) => (), - /// y @ _ => (), // easier written as `y`, - /// } - /// ``` - pub REDUNDANT_PATTERN, - style, - "using `name @ _` in a pattern" -} - declare_clippy_lint! { /// **What it does:** Checks for the use of bindings with a single leading /// underscore. @@ -247,7 +225,6 @@ declare_lint_pass!(MiscLints => [ FLOAT_CMP, CMP_OWNED, MODULO_ONE, - REDUNDANT_PATTERN, USED_UNDERSCORE_BINDING, SHORT_CIRCUIT_STATEMENT, ZERO_PTR, @@ -459,22 +436,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints { ); } } - - fn check_pat(&mut self, cx: &LateContext<'a, 'tcx>, pat: &'tcx Pat) { - if let PatKind::Binding(.., ident, Some(ref right)) = pat.node { - if let PatKind::Wild = right.node { - span_lint( - cx, - REDUNDANT_PATTERN, - pat.span, - &format!( - "the `{} @ _` pattern can be written as just `{}`", - ident.name, ident.name - ), - ); - } - } - } } fn check_nan(cx: &LateContext<'_, '_>, path: &Path, expr: &Expr) { diff --git a/clippy_lints/src/misc_early.rs b/clippy_lints/src/misc_early.rs index b90307af8ffc..f4a1a1e297fc 100644 --- a/clippy_lints/src/misc_early.rs +++ b/clippy_lints/src/misc_early.rs @@ -173,6 +173,28 @@ declare_clippy_lint! { "shadowing a builtin type" } +declare_clippy_lint! { + /// **What it does:** Checks for patterns in the form `name @ _`. + /// + /// **Why is this bad?** It's almost always more readable to just use direct + /// bindings. + /// + /// **Known problems:** None. + /// + /// **Example:** + /// ```rust + /// # let v = Some("abc"); + /// + /// match v { + /// Some(x) => (), + /// y @ _ => (), // easier written as `y`, + /// } + /// ``` + pub REDUNDANT_PATTERN, + style, + "using `name @ _` in a pattern" +} + declare_lint_pass!(MiscEarlyLints => [ UNNEEDED_FIELD_PATTERN, DUPLICATE_UNDERSCORE_ARGUMENT, @@ -181,7 +203,8 @@ declare_lint_pass!(MiscEarlyLints => [ MIXED_CASE_HEX_LITERALS, UNSEPARATED_LITERAL_SUFFIX, ZERO_PREFIXED_LITERAL, - BUILTIN_TYPE_SHADOW + BUILTIN_TYPE_SHADOW, + REDUNDANT_PATTERN ]); // Used to find `return` statements or equivalents e.g., `?` @@ -286,6 +309,20 @@ impl EarlyLintPass for MiscEarlyLints { } } } + + if let PatKind::Ident(_, ident, Some(ref right)) = pat.node { + if let PatKind::Wild = right.node { + span_lint( + cx, + REDUNDANT_PATTERN, + pat.span, + &format!( + "the `{} @ _` pattern can be written as just `{}`", + ident.name, ident.name, + ), + ); + } + } } fn check_fn(&mut self, cx: &EarlyContext<'_>, _: FnKind<'_>, decl: &FnDecl, _: Span, _: NodeId) { diff --git a/src/lintlist/mod.rs b/src/lintlist/mod.rs index 1435e9968dd9..223e6aa9acd7 100644 --- a/src/lintlist/mod.rs +++ b/src/lintlist/mod.rs @@ -1552,7 +1552,7 @@ pub const ALL_LINTS: [Lint; 313] = [ group: "style", desc: "using `name @ _` in a pattern", deprecation: None, - module: "misc", + module: "misc_early", }, Lint { name: "redundant_pattern_matching", diff --git a/tests/ui/patterns.rs b/tests/ui/patterns.rs index 576e6c9ab92a..f76be38a2fcc 100644 --- a/tests/ui/patterns.rs +++ b/tests/ui/patterns.rs @@ -1,8 +1,10 @@ #![allow(unused)] #![warn(clippy::all)] +#![feature(slice_patterns)] fn main() { let v = Some(true); + let s = [0, 1, 2, 3, 4]; match v { Some(x) => (), y @ _ => (), @@ -11,4 +13,8 @@ fn main() { Some(x) => (), y @ None => (), // no error } + match s { + [x, inside @ .., y] => (), // no error + [..] => (), + } } diff --git a/tests/ui/patterns.stderr b/tests/ui/patterns.stderr index 39dc034a0141..31dfe1cd11c5 100644 --- a/tests/ui/patterns.stderr +++ b/tests/ui/patterns.stderr @@ -1,5 +1,5 @@ error: the `y @ _` pattern can be written as just `y` - --> $DIR/patterns.rs:8:9 + --> $DIR/patterns.rs:10:9 | LL | y @ _ => (), | ^^^^^ From 8be37fdb1d50498bdc3a860dd07d79c847247d0c Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 5 Sep 2019 00:50:22 +0900 Subject: [PATCH 2/3] Make it auto fixable lint --- clippy_lints/src/misc_early.rs | 5 ++++- tests/ui/patterns.stderr | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/misc_early.rs b/clippy_lints/src/misc_early.rs index f4a1a1e297fc..47446edac54b 100644 --- a/clippy_lints/src/misc_early.rs +++ b/clippy_lints/src/misc_early.rs @@ -312,7 +312,7 @@ impl EarlyLintPass for MiscEarlyLints { if let PatKind::Ident(_, ident, Some(ref right)) = pat.node { if let PatKind::Wild = right.node { - span_lint( + span_lint_and_sugg( cx, REDUNDANT_PATTERN, pat.span, @@ -320,6 +320,9 @@ impl EarlyLintPass for MiscEarlyLints { "the `{} @ _` pattern can be written as just `{}`", ident.name, ident.name, ), + "try", + format!("{}", ident.name), + Applicability::MachineApplicable, ); } } diff --git a/tests/ui/patterns.stderr b/tests/ui/patterns.stderr index 31dfe1cd11c5..f25e71e872b2 100644 --- a/tests/ui/patterns.stderr +++ b/tests/ui/patterns.stderr @@ -2,7 +2,7 @@ error: the `y @ _` pattern can be written as just `y` --> $DIR/patterns.rs:10:9 | LL | y @ _ => (), - | ^^^^^ + | ^^^^^ help: try: `y` | = note: `-D clippy::redundant-pattern` implied by `-D warnings` From 0b3f4527641197510efe9bbe034c07c1bcf41d61 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 5 Sep 2019 22:45:52 +0900 Subject: [PATCH 3/3] Run rustfix --- tests/ui/patterns.fixed | 21 +++++++++++++++++++++ tests/ui/patterns.rs | 1 + tests/ui/patterns.stderr | 2 +- 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/ui/patterns.fixed diff --git a/tests/ui/patterns.fixed b/tests/ui/patterns.fixed new file mode 100644 index 000000000000..a443db7495dc --- /dev/null +++ b/tests/ui/patterns.fixed @@ -0,0 +1,21 @@ +// run-rustfix +#![allow(unused)] +#![warn(clippy::all)] +#![feature(slice_patterns)] + +fn main() { + let v = Some(true); + let s = [0, 1, 2, 3, 4]; + match v { + Some(x) => (), + y => (), + } + match v { + Some(x) => (), + y @ None => (), // no error + } + match s { + [x, inside @ .., y] => (), // no error + [..] => (), + } +} diff --git a/tests/ui/patterns.rs b/tests/ui/patterns.rs index f76be38a2fcc..2c9f839ecf68 100644 --- a/tests/ui/patterns.rs +++ b/tests/ui/patterns.rs @@ -1,3 +1,4 @@ +// run-rustfix #![allow(unused)] #![warn(clippy::all)] #![feature(slice_patterns)] diff --git a/tests/ui/patterns.stderr b/tests/ui/patterns.stderr index f25e71e872b2..784a3feaace6 100644 --- a/tests/ui/patterns.stderr +++ b/tests/ui/patterns.stderr @@ -1,5 +1,5 @@ error: the `y @ _` pattern can be written as just `y` - --> $DIR/patterns.rs:10:9 + --> $DIR/patterns.rs:11:9 | LL | y @ _ => (), | ^^^^^ help: try: `y`