Skip to content

Commit

Permalink
Auto merge of #4489 - JohnTitor:fix-redundant-pattern-false-positive,…
Browse files Browse the repository at this point in the history
… r=flip1995

Fix `redundant_pattern` false positive

Fix #4428

changelog: Fix `redundant_pattern` false positive
  • Loading branch information
bors committed Sep 5, 2019
2 parents 5f28fda + 0b3f452 commit 313b41f
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 45 deletions.
4 changes: 2 additions & 2 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
39 changes: 0 additions & 39 deletions clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -247,7 +225,6 @@ declare_lint_pass!(MiscLints => [
FLOAT_CMP,
CMP_OWNED,
MODULO_ONE,
REDUNDANT_PATTERN,
USED_UNDERSCORE_BINDING,
SHORT_CIRCUIT_STATEMENT,
ZERO_PTR,
Expand Down Expand Up @@ -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) {
Expand Down
42 changes: 41 additions & 1 deletion clippy_lints/src/misc_early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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., `?`
Expand Down Expand Up @@ -286,6 +309,23 @@ impl EarlyLintPass for MiscEarlyLints {
}
}
}

if let PatKind::Ident(_, ident, Some(ref right)) = pat.node {
if let PatKind::Wild = right.node {
span_lint_and_sugg(
cx,
REDUNDANT_PATTERN,
pat.span,
&format!(
"the `{} @ _` pattern can be written as just `{}`",
ident.name, ident.name,
),
"try",
format!("{}", ident.name),
Applicability::MachineApplicable,
);
}
}
}

fn check_fn(&mut self, cx: &EarlyContext<'_>, _: FnKind<'_>, decl: &FnDecl, _: Span, _: NodeId) {
Expand Down
2 changes: 1 addition & 1 deletion src/lintlist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/patterns.fixed
Original file line number Diff line number Diff line change
@@ -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
[..] => (),
}
}
7 changes: 7 additions & 0 deletions tests/ui/patterns.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// 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 @ _ => (),
Expand All @@ -11,4 +14,8 @@ fn main() {
Some(x) => (),
y @ None => (), // no error
}
match s {
[x, inside @ .., y] => (), // no error
[..] => (),
}
}
4 changes: 2 additions & 2 deletions tests/ui/patterns.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: the `y @ _` pattern can be written as just `y`
--> $DIR/patterns.rs:8:9
--> $DIR/patterns.rs:11:9
|
LL | y @ _ => (),
| ^^^^^
| ^^^^^ help: try: `y`
|
= note: `-D clippy::redundant-pattern` implied by `-D warnings`

Expand Down

0 comments on commit 313b41f

Please sign in to comment.