Skip to content

Commit 0fa0df9

Browse files
committed
Span help without suggestion
1 parent 8ae8b08 commit 0fa0df9

File tree

6 files changed

+23
-82
lines changed

6 files changed

+23
-82
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
88

9-
[There are 345 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
9+
[There are 346 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1010

1111
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1212

clippy_lints/src/matches.rs

+6-32
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use crate::utils::paths;
33
use crate::utils::sugg::Sugg;
44
use crate::utils::{
55
expr_block, is_allowed, is_expn_of, match_qpath, match_type, multispan_sugg, remove_blocks, snippet,
6-
snippet_with_applicability, span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty,
6+
snippet_with_applicability, span_help_and_lint, span_lint_and_sugg, span_lint_and_then, span_note_and_lint,
7+
walk_ptrs_ty,
78
};
89
use if_chain::if_chain;
910
use rustc::declare_lint_pass;
@@ -267,7 +268,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Matches {
267268
check_wild_err_arm(cx, ex, arms);
268269
check_wild_enum_match(cx, ex, arms);
269270
check_match_as_ref(cx, ex, arms, expr);
270-
check_wild_in_or_pats(cx, ex, arms);
271+
check_wild_in_or_pats(cx, arms);
271272
}
272273
if let ExprKind::Match(ref ex, ref arms, _) = expr.kind {
273274
check_match_ref_pats(cx, ex, arms, expr);
@@ -686,44 +687,17 @@ fn check_match_as_ref(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_>],
686687
}
687688
}
688689

689-
fn check_wild_in_or_pats(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
690-
let mut is_non_exhaustive_enum = false;
691-
let ty = cx.tables.expr_ty(ex);
692-
if ty.is_enum() {
693-
if let ty::Adt(def, _) = ty.kind {
694-
if def.is_variant_list_non_exhaustive() {
695-
is_non_exhaustive_enum = true;
696-
}
697-
}
698-
}
699-
690+
fn check_wild_in_or_pats(cx: &LateContext<'_, '_>, arms: &[Arm<'_>]) {
700691
for arm in arms {
701692
if let PatKind::Or(ref fields) = arm.pat.kind {
702693
// look for multiple fields in this arm that contains at least one Wild pattern
703694
if fields.len() > 1 && fields.iter().any(is_wild) {
704-
span_lint_and_then(
695+
span_help_and_lint(
705696
cx,
706697
WILDCARD_IN_OR_PATTERNS,
707698
arm.pat.span,
708699
"wildcard pattern covers any other pattern as it will match anyway.",
709-
|db| {
710-
// handle case where a non exhaustive enum is being used
711-
if is_non_exhaustive_enum {
712-
db.span_suggestion(
713-
arm.pat.span,
714-
"consider handling `_` separately.",
715-
"_ => ...".to_string(),
716-
Applicability::MaybeIncorrect,
717-
);
718-
} else {
719-
db.span_suggestion(
720-
arm.pat.span,
721-
"consider replacing with wildcard pattern only",
722-
"_".to_string(),
723-
Applicability::MachineApplicable,
724-
);
725-
}
726-
},
700+
"Consider handling `_` separately.",
727701
);
728702
}
729703
}

src/lintlist/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use lint::Lint;
66
pub use lint::LINT_LEVELS;
77

88
// begin lint list, do not remove this comment, it’s used in `update_lints`
9-
pub const ALL_LINTS: [Lint; 345] = [
9+
pub const ALL_LINTS: [Lint; 346] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",

tests/ui/wild_in_or_pats.fixed

-38
This file was deleted.

tests/ui/wild_in_or_pats.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// run-rustfix
2-
31
#![warn(clippy::wildcard_in_or_patterns)]
42

53
fn main() {

tests/ui/wild_in_or_pats.stderr

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
error: wildcard pattern covers any other pattern as it will match anyway.
2-
--> $DIR/wild_in_or_pats.rs:10:9
2+
--> $DIR/wild_in_or_pats.rs:8:9
33
|
44
LL | "bar" | _ => {
5-
| ^^^^^^^^^ help: consider replacing with wildcard pattern only: `_`
5+
| ^^^^^^^^^
66
|
77
= note: `-D clippy::wildcard-in-or-patterns` implied by `-D warnings`
8+
= help: Consider handling `_` separately.
89

910
error: wildcard pattern covers any other pattern as it will match anyway.
10-
--> $DIR/wild_in_or_pats.rs:18:9
11+
--> $DIR/wild_in_or_pats.rs:16:9
1112
|
1213
LL | "bar" | "bar2" | _ => {
13-
| ^^^^^^^^^^^^^^^^^^ help: consider replacing with wildcard pattern only: `_`
14+
| ^^^^^^^^^^^^^^^^^^
15+
|
16+
= help: Consider handling `_` separately.
1417

1518
error: wildcard pattern covers any other pattern as it will match anyway.
16-
--> $DIR/wild_in_or_pats.rs:26:9
19+
--> $DIR/wild_in_or_pats.rs:24:9
1720
|
1821
LL | _ | "bar" | _ => {
19-
| ^^^^^^^^^^^^^ help: consider replacing with wildcard pattern only: `_`
22+
| ^^^^^^^^^^^^^
23+
|
24+
= help: Consider handling `_` separately.
2025

2126
error: wildcard pattern covers any other pattern as it will match anyway.
22-
--> $DIR/wild_in_or_pats.rs:34:9
27+
--> $DIR/wild_in_or_pats.rs:32:9
2328
|
2429
LL | _ | "bar" => {
25-
| ^^^^^^^^^ help: consider replacing with wildcard pattern only: `_`
30+
| ^^^^^^^^^
31+
|
32+
= help: Consider handling `_` separately.
2633

2734
error: aborting due to 4 previous errors
2835

0 commit comments

Comments
 (0)