From 4a3bc6b5923217a53441b7745b9f5542ed4245e8 Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Thu, 12 Sep 2019 08:25:05 +0200 Subject: [PATCH 1/5] Add `unneeded-wildcard-pattern` lint --- CHANGELOG.md | 1 + README.md | 2 +- clippy_lints/src/lib.rs | 2 + clippy_lints/src/misc_early.rs | 93 ++++++++++++++++++++++- src/lintlist/mod.rs | 9 ++- tests/ui/unneeded_wildcard_pattern.fixed | 41 ++++++++++ tests/ui/unneeded_wildcard_pattern.rs | 41 ++++++++++ tests/ui/unneeded_wildcard_pattern.stderr | 68 +++++++++++++++++ 8 files changed, 254 insertions(+), 3 deletions(-) create mode 100644 tests/ui/unneeded_wildcard_pattern.fixed create mode 100644 tests/ui/unneeded_wildcard_pattern.rs create mode 100644 tests/ui/unneeded_wildcard_pattern.stderr diff --git a/CHANGELOG.md b/CHANGELOG.md index dbdf3df4ddc3..eb710654cafa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1188,6 +1188,7 @@ Released 2018-09-13 [`unnecessary_operation`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation [`unnecessary_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_unwrap [`unneeded_field_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#unneeded_field_pattern +[`unneeded_wildcard_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#unneeded_wildcard_pattern [`unreadable_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#unreadable_literal [`unsafe_removed_from_name`]: https://rust-lang.github.io/rust-clippy/master/index.html#unsafe_removed_from_name [`unsafe_vector_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#unsafe_vector_initialization diff --git a/README.md b/README.md index dd315fd397b0..4541af9c844e 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code. -[There are 313 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html) +[There are 314 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html) We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you: diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 9d0a91c53182..de4d262bddca 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -825,6 +825,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con misc_early::REDUNDANT_CLOSURE_CALL, misc_early::REDUNDANT_PATTERN, misc_early::UNNEEDED_FIELD_PATTERN, + misc_early::UNNEEDED_WILDCARD_PATTERN, misc_early::ZERO_PREFIXED_LITERAL, mut_reference::UNNECESSARY_MUT_PASSED, mutex_atomic::MUTEX_ATOMIC, @@ -1044,6 +1045,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con methods::USELESS_ASREF, misc::SHORT_CIRCUIT_STATEMENT, misc_early::REDUNDANT_CLOSURE_CALL, + misc_early::UNNEEDED_WILDCARD_PATTERN, misc_early::ZERO_PREFIXED_LITERAL, needless_bool::BOOL_COMPARISON, needless_bool::NEEDLESS_BOOL, diff --git a/clippy_lints/src/misc_early.rs b/clippy_lints/src/misc_early.rs index a9907ed132d0..2e1be755d098 100644 --- a/clippy_lints/src/misc_early.rs +++ b/clippy_lints/src/misc_early.rs @@ -195,6 +195,40 @@ declare_clippy_lint! { "using `name @ _` in a pattern" } +declare_clippy_lint! { + /// **What it does:** Checks for tuple patterns with a wildcard + /// pattern (`_`) is next to a rest pattern (`..`) pattern. + /// + /// **Why is this bad?** The wildcard pattern is unneeded as the rest pattern + /// can match that element as well. + /// + /// **Known problems:** None. + /// + /// **Example:** + /// ```rust + /// # struct TupleStruct(u32, u32, u32); + /// # let t = TupleStruct(1, 2, 3); + /// + /// match t { + /// TupleStruct(0, .., _) => (), + /// _ => (), + /// } + /// ``` + /// can be written as + /// ```rust + /// # struct TupleStruct(u32, u32, u32); + /// # let t = TupleStruct(1, 2, 3); + /// + /// match t { + /// TupleStruct(0, ..) => (), + /// _ => (), + /// } + /// ``` + pub UNNEEDED_WILDCARD_PATTERN, + complexity, + "tuple patterns with a wildcard pattern (`_`) is next to a rest pattern (`..`) pattern" +} + declare_lint_pass!(MiscEarlyLints => [ UNNEEDED_FIELD_PATTERN, DUPLICATE_UNDERSCORE_ARGUMENT, @@ -204,7 +238,8 @@ declare_lint_pass!(MiscEarlyLints => [ UNSEPARATED_LITERAL_SUFFIX, ZERO_PREFIXED_LITERAL, BUILTIN_TYPE_SHADOW, - REDUNDANT_PATTERN + REDUNDANT_PATTERN, + UNNEEDED_WILDCARD_PATTERN, ]); // Used to find `return` statements or equivalents e.g., `?` @@ -326,6 +361,62 @@ impl EarlyLintPass for MiscEarlyLints { ); } } + + if let PatKind::TupleStruct(_, ref patterns) | PatKind::Tuple(ref patterns) = pat.node { + fn span_lint(cx: &EarlyContext<'_>, span: Span, only_one: bool) { + span_lint_and_sugg( + cx, + UNNEEDED_WILDCARD_PATTERN, + span, + if only_one { + "this pattern is unneeded as the `..` pattern can match that element" + } else { + "these patterns are unneeded as the `..` pattern can match those elements" + }, + if only_one { "remove it" } else { "remove them" }, + "".to_string(), + Applicability::MachineApplicable, + ); + } + + fn is_rest>(pat: &P) -> bool { + if let PatKind::Rest = pat.node { + true + } else { + false + } + } + + fn is_wild>(pat: &&P) -> bool { + if let PatKind::Wild = pat.node { + true + } else { + false + } + } + + if let Some(rest_index) = patterns.iter().position(is_rest) { + if let Some((left_index, left_pat)) = patterns[..rest_index] + .iter() + .rev() + .take_while(is_wild) + .enumerate() + .last() + { + span_lint(cx, left_pat.span.until(patterns[rest_index].span), left_index == 0); + } + + if let Some((right_index, right_pat)) = + patterns[rest_index + 1..].iter().take_while(is_wild).enumerate().last() + { + span_lint( + cx, + patterns[rest_index].span.shrink_to_hi().to(right_pat.span), + right_index == 0, + ); + } + } + } } 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 223e6aa9acd7..589ae6b3d359 100644 --- a/src/lintlist/mod.rs +++ b/src/lintlist/mod.rs @@ -6,7 +6,7 @@ pub use lint::Lint; pub use lint::LINT_LEVELS; // begin lint list, do not remove this comment, it’s used in `update_lints` -pub const ALL_LINTS: [Lint; 313] = [ +pub const ALL_LINTS: [Lint; 314] = [ Lint { name: "absurd_extreme_comparisons", group: "correctness", @@ -1974,6 +1974,13 @@ pub const ALL_LINTS: [Lint; 313] = [ deprecation: None, module: "misc_early", }, + Lint { + name: "unneeded_wildcard_pattern", + group: "complexity", + desc: "tuple patterns with a wildcard pattern (`_`) is next to a rest pattern (`..`) pattern", + deprecation: None, + module: "misc_early", + }, Lint { name: "unreadable_literal", group: "style", diff --git a/tests/ui/unneeded_wildcard_pattern.fixed b/tests/ui/unneeded_wildcard_pattern.fixed new file mode 100644 index 000000000000..d0b62fa6f435 --- /dev/null +++ b/tests/ui/unneeded_wildcard_pattern.fixed @@ -0,0 +1,41 @@ +// run-rustfix +#![feature(stmt_expr_attributes)] +#![deny(clippy::unneeded_wildcard_pattern)] + +fn main() { + let t = (0, 1, 2, 3); + + if let (0, ..) = t {}; + if let (0, ..) = t {}; + if let (0, ..) = t {}; + if let (0, ..) = t {}; + if let (_, 0, ..) = t {}; + if let (.., 0, _) = t {}; + if let (0, _, _, _) = t {}; + if let (0, ..) = t {}; + if let (.., 0) = t {}; + + #[rustfmt::skip] + { + if let (0, ..,) = t {}; + } + + struct S(usize, usize, usize, usize); + + let s = S(0, 1, 2, 3); + + if let S(0, ..) = s {}; + if let S(0, ..) = s {}; + if let S(0, ..) = s {}; + if let S(0, ..) = s {}; + if let S(_, 0, ..) = s {}; + if let S(.., 0, _) = s {}; + if let S(0, _, _, _) = s {}; + if let S(0, ..) = s {}; + if let S(.., 0) = s {}; + + #[rustfmt::skip] + { + if let S(0, ..,) = s {}; + } +} diff --git a/tests/ui/unneeded_wildcard_pattern.rs b/tests/ui/unneeded_wildcard_pattern.rs new file mode 100644 index 000000000000..bad158907ba9 --- /dev/null +++ b/tests/ui/unneeded_wildcard_pattern.rs @@ -0,0 +1,41 @@ +// run-rustfix +#![feature(stmt_expr_attributes)] +#![deny(clippy::unneeded_wildcard_pattern)] + +fn main() { + let t = (0, 1, 2, 3); + + if let (0, .., _) = t {}; + if let (0, _, ..) = t {}; + if let (0, _, _, ..) = t {}; + if let (0, .., _, _) = t {}; + if let (_, 0, ..) = t {}; + if let (.., 0, _) = t {}; + if let (0, _, _, _) = t {}; + if let (0, ..) = t {}; + if let (.., 0) = t {}; + + #[rustfmt::skip] + { + if let (0, .., _, _,) = t {}; + } + + struct S(usize, usize, usize, usize); + + let s = S(0, 1, 2, 3); + + if let S(0, .., _) = s {}; + if let S(0, _, ..) = s {}; + if let S(0, _, _, ..) = s {}; + if let S(0, .., _, _) = s {}; + if let S(_, 0, ..) = s {}; + if let S(.., 0, _) = s {}; + if let S(0, _, _, _) = s {}; + if let S(0, ..) = s {}; + if let S(.., 0) = s {}; + + #[rustfmt::skip] + { + if let S(0, .., _, _,) = s {}; + } +} diff --git a/tests/ui/unneeded_wildcard_pattern.stderr b/tests/ui/unneeded_wildcard_pattern.stderr new file mode 100644 index 000000000000..8cc2516959a3 --- /dev/null +++ b/tests/ui/unneeded_wildcard_pattern.stderr @@ -0,0 +1,68 @@ +error: this pattern is unneeded as the `..` pattern can match that element + --> $DIR/unneeded_wildcard_pattern.rs:8:18 + | +LL | if let (0, .., _) = t {}; + | ^^^ help: remove it + | +note: lint level defined here + --> $DIR/unneeded_wildcard_pattern.rs:3:9 + | +LL | #![deny(clippy::unneeded_wildcard_pattern)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: this pattern is unneeded as the `..` pattern can match that element + --> $DIR/unneeded_wildcard_pattern.rs:9:16 + | +LL | if let (0, _, ..) = t {}; + | ^^^ help: remove it + +error: these patterns are unneeded as the `..` pattern can match those elements + --> $DIR/unneeded_wildcard_pattern.rs:10:16 + | +LL | if let (0, _, _, ..) = t {}; + | ^^^^^^ help: remove them + +error: these patterns are unneeded as the `..` pattern can match those elements + --> $DIR/unneeded_wildcard_pattern.rs:11:18 + | +LL | if let (0, .., _, _) = t {}; + | ^^^^^^ help: remove them + +error: these patterns are unneeded as the `..` pattern can match those elements + --> $DIR/unneeded_wildcard_pattern.rs:20:22 + | +LL | if let (0, .., _, _,) = t {}; + | ^^^^^^ help: remove them + +error: this pattern is unneeded as the `..` pattern can match that element + --> $DIR/unneeded_wildcard_pattern.rs:27:19 + | +LL | if let S(0, .., _) = s {}; + | ^^^ help: remove it + +error: this pattern is unneeded as the `..` pattern can match that element + --> $DIR/unneeded_wildcard_pattern.rs:28:17 + | +LL | if let S(0, _, ..) = s {}; + | ^^^ help: remove it + +error: these patterns are unneeded as the `..` pattern can match those elements + --> $DIR/unneeded_wildcard_pattern.rs:29:17 + | +LL | if let S(0, _, _, ..) = s {}; + | ^^^^^^ help: remove them + +error: these patterns are unneeded as the `..` pattern can match those elements + --> $DIR/unneeded_wildcard_pattern.rs:30:19 + | +LL | if let S(0, .., _, _) = s {}; + | ^^^^^^ help: remove them + +error: these patterns are unneeded as the `..` pattern can match those elements + --> $DIR/unneeded_wildcard_pattern.rs:39:23 + | +LL | if let S(0, .., _, _,) = s {}; + | ^^^^^^ help: remove them + +error: aborting due to 10 previous errors + From 00ca42fe5bcd1eb412f4486237abf4ae586de99a Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Thu, 12 Sep 2019 08:36:05 +0200 Subject: [PATCH 2/5] Fix lint warnings --- clippy_lints/src/eval_order_dependence.rs | 2 +- clippy_lints/src/misc_early.rs | 115 +++++++++++----------- clippy_lints/src/no_effect.rs | 2 +- clippy_lints/src/utils/sugg.rs | 2 +- 4 files changed, 63 insertions(+), 58 deletions(-) diff --git a/clippy_lints/src/eval_order_dependence.rs b/clippy_lints/src/eval_order_dependence.rs index b915d50671b3..efd4e1e0334c 100644 --- a/clippy_lints/src/eval_order_dependence.rs +++ b/clippy_lints/src/eval_order_dependence.rs @@ -101,7 +101,7 @@ struct DivergenceVisitor<'a, 'tcx> { impl<'a, 'tcx> DivergenceVisitor<'a, 'tcx> { fn maybe_walk_expr(&mut self, e: &'tcx Expr) { match e.node { - ExprKind::Closure(.., _) => {}, + ExprKind::Closure(..) => {}, ExprKind::Match(ref e, ref arms, _) => { self.visit_expr(e); for arm in arms { diff --git a/clippy_lints/src/misc_early.rs b/clippy_lints/src/misc_early.rs index 2e1be755d098..17c44d148b46 100644 --- a/clippy_lints/src/misc_early.rs +++ b/clippy_lints/src/misc_early.rs @@ -362,61 +362,7 @@ impl EarlyLintPass for MiscEarlyLints { } } - if let PatKind::TupleStruct(_, ref patterns) | PatKind::Tuple(ref patterns) = pat.node { - fn span_lint(cx: &EarlyContext<'_>, span: Span, only_one: bool) { - span_lint_and_sugg( - cx, - UNNEEDED_WILDCARD_PATTERN, - span, - if only_one { - "this pattern is unneeded as the `..` pattern can match that element" - } else { - "these patterns are unneeded as the `..` pattern can match those elements" - }, - if only_one { "remove it" } else { "remove them" }, - "".to_string(), - Applicability::MachineApplicable, - ); - } - - fn is_rest>(pat: &P) -> bool { - if let PatKind::Rest = pat.node { - true - } else { - false - } - } - - fn is_wild>(pat: &&P) -> bool { - if let PatKind::Wild = pat.node { - true - } else { - false - } - } - - if let Some(rest_index) = patterns.iter().position(is_rest) { - if let Some((left_index, left_pat)) = patterns[..rest_index] - .iter() - .rev() - .take_while(is_wild) - .enumerate() - .last() - { - span_lint(cx, left_pat.span.until(patterns[rest_index].span), left_index == 0); - } - - if let Some((right_index, right_pat)) = - patterns[rest_index + 1..].iter().take_while(is_wild).enumerate().last() - { - span_lint( - cx, - patterns[rest_index].span.shrink_to_hi().to(right_pat.span), - right_index == 0, - ); - } - } - } + check_unneeded_wildcard_pattern(cx, pat); } fn check_fn(&mut self, cx: &EarlyContext<'_>, _: FnKind<'_>, decl: &FnDecl, _: Span, _: NodeId) { @@ -611,3 +557,62 @@ impl MiscEarlyLints { } } } + +fn check_unneeded_wildcard_pattern(cx: &EarlyContext<'_>, pat: &Pat) { + if let PatKind::TupleStruct(_, ref patterns) | PatKind::Tuple(ref patterns) = pat.node { + fn span_lint(cx: &EarlyContext<'_>, span: Span, only_one: bool) { + span_lint_and_sugg( + cx, + UNNEEDED_WILDCARD_PATTERN, + span, + if only_one { + "this pattern is unneeded as the `..` pattern can match that element" + } else { + "these patterns are unneeded as the `..` pattern can match those elements" + }, + if only_one { "remove it" } else { "remove them" }, + "".to_string(), + Applicability::MachineApplicable, + ); + } + + fn is_rest>(pat: &P) -> bool { + if let PatKind::Rest = pat.node { + true + } else { + false + } + } + + #[allow(clippy::trivially_copy_pass_by_ref)] + fn is_wild>(pat: &&P) -> bool { + if let PatKind::Wild = pat.node { + true + } else { + false + } + } + + if let Some(rest_index) = patterns.iter().position(is_rest) { + if let Some((left_index, left_pat)) = patterns[..rest_index] + .iter() + .rev() + .take_while(is_wild) + .enumerate() + .last() + { + span_lint(cx, left_pat.span.until(patterns[rest_index].span), left_index == 0); + } + + if let Some((right_index, right_pat)) = + patterns[rest_index + 1..].iter().take_while(is_wild).enumerate().last() + { + span_lint( + cx, + patterns[rest_index].span.shrink_to_hi().to(right_pat.span), + right_index == 0, + ); + } + } + } +} diff --git a/clippy_lints/src/no_effect.rs b/clippy_lints/src/no_effect.rs index 576446d35a76..22129a3abb94 100644 --- a/clippy_lints/src/no_effect.rs +++ b/clippy_lints/src/no_effect.rs @@ -47,7 +47,7 @@ fn has_no_effect(cx: &LateContext<'_, '_>, expr: &Expr) -> bool { return false; } match expr.node { - ExprKind::Lit(..) | ExprKind::Closure(.., _) => true, + ExprKind::Lit(..) | ExprKind::Closure(..) => true, ExprKind::Path(..) => !has_drop(cx, cx.tables.expr_ty(expr)), ExprKind::Index(ref a, ref b) | ExprKind::Binary(_, ref a, ref b) => { has_no_effect(cx, a) && has_no_effect(cx, b) diff --git a/clippy_lints/src/utils/sugg.rs b/clippy_lints/src/utils/sugg.rs index b1b5791b15d8..249df8e0e230 100644 --- a/clippy_lints/src/utils/sugg.rs +++ b/clippy_lints/src/utils/sugg.rs @@ -93,7 +93,7 @@ impl<'a> Sugg<'a> { match expr.node { hir::ExprKind::AddrOf(..) | hir::ExprKind::Box(..) - | hir::ExprKind::Closure(.., _) + | hir::ExprKind::Closure(..) | hir::ExprKind::Unary(..) | hir::ExprKind::Match(..) => Sugg::MaybeParen(snippet), hir::ExprKind::Continue(..) From 99be5221bcb958dd8532ac34ba9166d8c784a341 Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Fri, 13 Sep 2019 08:20:27 +0200 Subject: [PATCH 3/5] Apply review suggestions Fix grammar errors and use `Pat::is_rest` instead of own function. --- clippy_lints/src/misc_early.rs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/clippy_lints/src/misc_early.rs b/clippy_lints/src/misc_early.rs index 17c44d148b46..d991d499764c 100644 --- a/clippy_lints/src/misc_early.rs +++ b/clippy_lints/src/misc_early.rs @@ -197,7 +197,7 @@ declare_clippy_lint! { declare_clippy_lint! { /// **What it does:** Checks for tuple patterns with a wildcard - /// pattern (`_`) is next to a rest pattern (`..`) pattern. + /// pattern (`_`) is next to a rest pattern (`..`). /// /// **Why is this bad?** The wildcard pattern is unneeded as the rest pattern /// can match that element as well. @@ -226,7 +226,7 @@ declare_clippy_lint! { /// ``` pub UNNEEDED_WILDCARD_PATTERN, complexity, - "tuple patterns with a wildcard pattern (`_`) is next to a rest pattern (`..`) pattern" + "tuple patterns with a wildcard pattern (`_`) is next to a rest pattern (`..`)" } declare_lint_pass!(MiscEarlyLints => [ @@ -576,14 +576,6 @@ fn check_unneeded_wildcard_pattern(cx: &EarlyContext<'_>, pat: &Pat) { ); } - fn is_rest>(pat: &P) -> bool { - if let PatKind::Rest = pat.node { - true - } else { - false - } - } - #[allow(clippy::trivially_copy_pass_by_ref)] fn is_wild>(pat: &&P) -> bool { if let PatKind::Wild = pat.node { @@ -593,7 +585,7 @@ fn check_unneeded_wildcard_pattern(cx: &EarlyContext<'_>, pat: &Pat) { } } - if let Some(rest_index) = patterns.iter().position(is_rest) { + if let Some(rest_index) = patterns.iter().position(|pat| pat.is_rest()) { if let Some((left_index, left_pat)) = patterns[..rest_index] .iter() .rev() From fed1709f46791c0b46b6460740d15f0f5b89d8d5 Mon Sep 17 00:00:00 2001 From: mikerite <33983332+mikerite@users.noreply.github.com> Date: Wed, 18 Sep 2019 06:48:47 +0200 Subject: [PATCH 4/5] Add note about refactoring Co-Authored-By: Philipp Krones --- clippy_lints/src/misc_early.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/clippy_lints/src/misc_early.rs b/clippy_lints/src/misc_early.rs index d991d499764c..acc83d28897a 100644 --- a/clippy_lints/src/misc_early.rs +++ b/clippy_lints/src/misc_early.rs @@ -199,6 +199,10 @@ declare_clippy_lint! { /// **What it does:** Checks for tuple patterns with a wildcard /// pattern (`_`) is next to a rest pattern (`..`). /// + /// _NOTE_: While `_, ..` means there is at least one element left, `..` + /// means there are 0 or more elements left. This can make a difference + /// when refactoring, but shouldn't result in errors in the refactored code, + /// since the wildcard pattern isn't used anyway. /// **Why is this bad?** The wildcard pattern is unneeded as the rest pattern /// can match that element as well. /// From be4e41562a7f1a14783fecff8dd904fd28d1f82f Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Sun, 22 Sep 2019 09:10:39 +0200 Subject: [PATCH 5/5] Add additional tests to unneeded_wildcard_pattern --- tests/ui/unneeded_wildcard_pattern.fixed | 4 +++ tests/ui/unneeded_wildcard_pattern.rs | 4 +++ tests/ui/unneeded_wildcard_pattern.stderr | 42 ++++++++++++++++++----- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/tests/ui/unneeded_wildcard_pattern.fixed b/tests/ui/unneeded_wildcard_pattern.fixed index d0b62fa6f435..12c3461c9557 100644 --- a/tests/ui/unneeded_wildcard_pattern.fixed +++ b/tests/ui/unneeded_wildcard_pattern.fixed @@ -7,6 +7,8 @@ fn main() { if let (0, ..) = t {}; if let (0, ..) = t {}; + if let (.., 0) = t {}; + if let (.., 0) = t {}; if let (0, ..) = t {}; if let (0, ..) = t {}; if let (_, 0, ..) = t {}; @@ -26,6 +28,8 @@ fn main() { if let S(0, ..) = s {}; if let S(0, ..) = s {}; + if let S(.., 0) = s {}; + if let S(.., 0) = s {}; if let S(0, ..) = s {}; if let S(0, ..) = s {}; if let S(_, 0, ..) = s {}; diff --git a/tests/ui/unneeded_wildcard_pattern.rs b/tests/ui/unneeded_wildcard_pattern.rs index bad158907ba9..4ac01d5d23b0 100644 --- a/tests/ui/unneeded_wildcard_pattern.rs +++ b/tests/ui/unneeded_wildcard_pattern.rs @@ -7,6 +7,8 @@ fn main() { if let (0, .., _) = t {}; if let (0, _, ..) = t {}; + if let (_, .., 0) = t {}; + if let (.., _, 0) = t {}; if let (0, _, _, ..) = t {}; if let (0, .., _, _) = t {}; if let (_, 0, ..) = t {}; @@ -26,6 +28,8 @@ fn main() { if let S(0, .., _) = s {}; if let S(0, _, ..) = s {}; + if let S(_, .., 0) = s {}; + if let S(.., _, 0) = s {}; if let S(0, _, _, ..) = s {}; if let S(0, .., _, _) = s {}; if let S(_, 0, ..) = s {}; diff --git a/tests/ui/unneeded_wildcard_pattern.stderr b/tests/ui/unneeded_wildcard_pattern.stderr index 8cc2516959a3..25251cf36c0f 100644 --- a/tests/ui/unneeded_wildcard_pattern.stderr +++ b/tests/ui/unneeded_wildcard_pattern.stderr @@ -16,53 +16,77 @@ error: this pattern is unneeded as the `..` pattern can match that element LL | if let (0, _, ..) = t {}; | ^^^ help: remove it +error: this pattern is unneeded as the `..` pattern can match that element + --> $DIR/unneeded_wildcard_pattern.rs:10:13 + | +LL | if let (_, .., 0) = t {}; + | ^^^ help: remove it + +error: this pattern is unneeded as the `..` pattern can match that element + --> $DIR/unneeded_wildcard_pattern.rs:11:15 + | +LL | if let (.., _, 0) = t {}; + | ^^^ help: remove it + error: these patterns are unneeded as the `..` pattern can match those elements - --> $DIR/unneeded_wildcard_pattern.rs:10:16 + --> $DIR/unneeded_wildcard_pattern.rs:12:16 | LL | if let (0, _, _, ..) = t {}; | ^^^^^^ help: remove them error: these patterns are unneeded as the `..` pattern can match those elements - --> $DIR/unneeded_wildcard_pattern.rs:11:18 + --> $DIR/unneeded_wildcard_pattern.rs:13:18 | LL | if let (0, .., _, _) = t {}; | ^^^^^^ help: remove them error: these patterns are unneeded as the `..` pattern can match those elements - --> $DIR/unneeded_wildcard_pattern.rs:20:22 + --> $DIR/unneeded_wildcard_pattern.rs:22:22 | LL | if let (0, .., _, _,) = t {}; | ^^^^^^ help: remove them error: this pattern is unneeded as the `..` pattern can match that element - --> $DIR/unneeded_wildcard_pattern.rs:27:19 + --> $DIR/unneeded_wildcard_pattern.rs:29:19 | LL | if let S(0, .., _) = s {}; | ^^^ help: remove it error: this pattern is unneeded as the `..` pattern can match that element - --> $DIR/unneeded_wildcard_pattern.rs:28:17 + --> $DIR/unneeded_wildcard_pattern.rs:30:17 | LL | if let S(0, _, ..) = s {}; | ^^^ help: remove it +error: this pattern is unneeded as the `..` pattern can match that element + --> $DIR/unneeded_wildcard_pattern.rs:31:14 + | +LL | if let S(_, .., 0) = s {}; + | ^^^ help: remove it + +error: this pattern is unneeded as the `..` pattern can match that element + --> $DIR/unneeded_wildcard_pattern.rs:32:16 + | +LL | if let S(.., _, 0) = s {}; + | ^^^ help: remove it + error: these patterns are unneeded as the `..` pattern can match those elements - --> $DIR/unneeded_wildcard_pattern.rs:29:17 + --> $DIR/unneeded_wildcard_pattern.rs:33:17 | LL | if let S(0, _, _, ..) = s {}; | ^^^^^^ help: remove them error: these patterns are unneeded as the `..` pattern can match those elements - --> $DIR/unneeded_wildcard_pattern.rs:30:19 + --> $DIR/unneeded_wildcard_pattern.rs:34:19 | LL | if let S(0, .., _, _) = s {}; | ^^^^^^ help: remove them error: these patterns are unneeded as the `..` pattern can match those elements - --> $DIR/unneeded_wildcard_pattern.rs:39:23 + --> $DIR/unneeded_wildcard_pattern.rs:43:23 | LL | if let S(0, .., _, _,) = s {}; | ^^^^^^ help: remove them -error: aborting due to 10 previous errors +error: aborting due to 14 previous errors