Skip to content

Commit

Permalink
Span help without suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibsG committed Jan 7, 2020
1 parent ad16718 commit 596f208
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 80 deletions.
38 changes: 6 additions & 32 deletions clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.",
);
}
}
Expand Down
38 changes: 0 additions & 38 deletions tests/ui/wild_in_or_pats.fixed

This file was deleted.

2 changes: 0 additions & 2 deletions tests/ui/wild_in_or_pats.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// run-rustfix

#![warn(clippy::wildcard_in_or_patterns)]

fn main() {
Expand Down
23 changes: 15 additions & 8 deletions tests/ui/wild_in_or_pats.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 596f208

Please sign in to comment.