Span help without suggestion

This commit is contained in:
ThibsG 2020-01-07 18:13:31 +01:00
parent 8ae8b08e32
commit 0fa0df9efb
6 changed files with 23 additions and 82 deletions

View file

@ -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 345 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
[There are 346 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:

View file

@ -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.",
);
}
}

View file

@ -6,7 +6,7 @@ pub use lint::Lint;
pub use lint::LINT_LEVELS;
// begin lint list, do not remove this comment, its used in `update_lints`
pub const ALL_LINTS: [Lint; 345] = [
pub const ALL_LINTS: [Lint; 346] = [
Lint {
name: "absurd_extreme_comparisons",
group: "correctness",

View file

@ -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");
},
};
}

View file

@ -1,5 +1,3 @@
// run-rustfix
#![warn(clippy::wildcard_in_or_patterns)]
fn main() {

View file

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