Fix redundant_pattern false positive

This commit is contained in:
Yuki Okushi 2019-09-03 04:49:14 +09:00
parent 9d2772207e
commit e236740f28
6 changed files with 48 additions and 44 deletions

View file

@ -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,

View file

@ -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) {

View file

@ -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) {

View file

@ -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",

View file

@ -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
[..] => (),
}
}

View file

@ -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 @ _ => (),
| ^^^^^