diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index 2e25b7236d75..6b5b4e4c4f0b 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -3,7 +3,8 @@ use crate::utils::paths; use crate::utils::sugg::Sugg; use crate::utils::{ expr_block, is_allowed, is_expn_of, match_qpath, match_type, multispan_sugg, remove_blocks, snippet, - snippet_with_applicability, span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty, + snippet_with_applicability, span_help_and_lint, span_lint_and_sugg, span_lint_and_then, span_note_and_lint, + walk_ptrs_ty, }; use if_chain::if_chain; use rustc::declare_lint_pass; @@ -267,7 +268,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Matches { check_wild_err_arm(cx, ex, arms); check_wild_enum_match(cx, ex, arms); check_match_as_ref(cx, ex, arms, expr); - check_wild_in_or_pats(cx, ex, arms); + check_wild_in_or_pats(cx, arms); } if let ExprKind::Match(ref ex, ref arms, _) = expr.kind { check_match_ref_pats(cx, ex, arms, expr); @@ -686,44 +687,17 @@ fn check_match_as_ref(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_>], } } -fn check_wild_in_or_pats(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_>]) { - let mut is_non_exhaustive_enum = false; - let ty = cx.tables.expr_ty(ex); - if ty.is_enum() { - if let ty::Adt(def, _) = ty.kind { - if def.is_variant_list_non_exhaustive() { - is_non_exhaustive_enum = true; - } - } - } - +fn check_wild_in_or_pats(cx: &LateContext<'_, '_>, arms: &[Arm<'_>]) { for arm in arms { if let PatKind::Or(ref fields) = arm.pat.kind { // look for multiple fields in this arm that contains at least one Wild pattern if fields.len() > 1 && fields.iter().any(is_wild) { - span_lint_and_then( + span_help_and_lint( cx, WILDCARD_IN_OR_PATTERNS, arm.pat.span, "wildcard pattern covers any other pattern as it will match anyway.", - |db| { - // handle case where a non exhaustive enum is being used - if is_non_exhaustive_enum { - db.span_suggestion( - arm.pat.span, - "consider handling `_` separately.", - "_ => ...".to_string(), - Applicability::MaybeIncorrect, - ); - } else { - db.span_suggestion( - arm.pat.span, - "consider replacing with wildcard pattern only", - "_".to_string(), - Applicability::MachineApplicable, - ); - } - }, + "Consider handling `_` separately.", ); } } diff --git a/src/lintlist/mod.rs b/src/lintlist/mod.rs index 399f3483a59b..3cc4efb9b057 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; 345] = [ +pub const ALL_LINTS: [Lint; 346] = [ Lint { name: "absurd_extreme_comparisons", group: "correctness", diff --git a/tests/ui/wild_in_or_pats.fixed b/tests/ui/wild_in_or_pats.fixed deleted file mode 100644 index 40d4a6e8fac5..000000000000 --- a/tests/ui/wild_in_or_pats.fixed +++ /dev/null @@ -1,38 +0,0 @@ -// run-rustfix - -#![warn(clippy::wildcard_in_or_patterns)] - -fn main() { - match "foo" { - "a" => { - dbg!("matched a"); - }, - _ => { - dbg!("matched (bar or) wild"); - }, - }; - match "foo" { - "a" => { - dbg!("matched a"); - }, - _ => { - dbg!("matched (bar or bar2 or) wild"); - }, - }; - match "foo" { - "a" => { - dbg!("matched a"); - }, - _ => { - dbg!("matched (bar or) wild"); - }, - }; - match "foo" { - "a" => { - dbg!("matched a"); - }, - _ => { - dbg!("matched (bar or) wild"); - }, - }; -} diff --git a/tests/ui/wild_in_or_pats.rs b/tests/ui/wild_in_or_pats.rs index 569871db936f..ad600f125772 100644 --- a/tests/ui/wild_in_or_pats.rs +++ b/tests/ui/wild_in_or_pats.rs @@ -1,5 +1,3 @@ -// run-rustfix - #![warn(clippy::wildcard_in_or_patterns)] fn main() { diff --git a/tests/ui/wild_in_or_pats.stderr b/tests/ui/wild_in_or_pats.stderr index 45ae31db21a9..33c34cbbd408 100644 --- a/tests/ui/wild_in_or_pats.stderr +++ b/tests/ui/wild_in_or_pats.stderr @@ -1,28 +1,35 @@ error: wildcard pattern covers any other pattern as it will match anyway. - --> $DIR/wild_in_or_pats.rs:10:9 + --> $DIR/wild_in_or_pats.rs:8:9 | LL | "bar" | _ => { - | ^^^^^^^^^ help: consider replacing with wildcard pattern only: `_` + | ^^^^^^^^^ | = note: `-D clippy::wildcard-in-or-patterns` implied by `-D warnings` + = help: Consider handling `_` separately. error: wildcard pattern covers any other pattern as it will match anyway. - --> $DIR/wild_in_or_pats.rs:18:9 + --> $DIR/wild_in_or_pats.rs:16:9 | LL | "bar" | "bar2" | _ => { - | ^^^^^^^^^^^^^^^^^^ help: consider replacing with wildcard pattern only: `_` + | ^^^^^^^^^^^^^^^^^^ + | + = help: Consider handling `_` separately. error: wildcard pattern covers any other pattern as it will match anyway. - --> $DIR/wild_in_or_pats.rs:26:9 + --> $DIR/wild_in_or_pats.rs:24:9 | LL | _ | "bar" | _ => { - | ^^^^^^^^^^^^^ help: consider replacing with wildcard pattern only: `_` + | ^^^^^^^^^^^^^ + | + = help: Consider handling `_` separately. error: wildcard pattern covers any other pattern as it will match anyway. - --> $DIR/wild_in_or_pats.rs:34:9 + --> $DIR/wild_in_or_pats.rs:32:9 | LL | _ | "bar" => { - | ^^^^^^^^^ help: consider replacing with wildcard pattern only: `_` + | ^^^^^^^^^ + | + = help: Consider handling `_` separately. error: aborting due to 4 previous errors