Skip to content

Commit cd3dcf9

Browse files
committed
Handle case for non-exhaustive enums
1 parent 97546f1 commit cd3dcf9

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

clippy_lints/src/matches.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Matches {
267267
check_wild_err_arm(cx, ex, arms);
268268
check_wild_enum_match(cx, ex, arms);
269269
check_match_as_ref(cx, ex, arms, expr);
270-
check_pats_wild_match(cx, arms);
270+
check_pats_wild_match(cx, ex, arms);
271271
}
272272
if let ExprKind::Match(ref ex, ref arms, _) = expr.kind {
273273
check_match_ref_pats(cx, ex, arms, expr);
@@ -686,20 +686,45 @@ fn check_match_as_ref(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_>],
686686
}
687687
}
688688

689-
fn check_pats_wild_match(cx: &LateContext<'_, '_>, arms: &[Arm]) {
689+
fn check_pats_wild_match(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+
690700
for arm in arms {
691701
if let PatKind::Or(ref fields) = arm.pat.kind {
692-
// look for multiple fields where one at least matches Wild pattern
693-
if fields.len() > 1 && fields.into_iter().any(is_wild) {
694-
span_lint_and_sugg(
702+
// look for multiple fields in this arm that contains at least one Wild pattern
703+
if fields.len() > 1 && fields.iter().any(is_wild) {
704+
span_lint_and_then(
695705
cx,
696706
PATS_WITH_WILD_MATCH_ARM,
697707
arm.pat.span,
698-
"wildcard pattern covers any other pattern as it will match anyway. Consider replacing with wildcard pattern only",
699-
"try this",
700-
"_".to_string(),
701-
Applicability::MachineApplicable,
702-
)
708+
"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+
},
727+
);
703728
}
704729
}
705730
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
error: wildcard pattern covers any other pattern as it will match anyway. Consider replacing with wildcard pattern only
1+
error: wildcard pattern covers any other pattern as it will match anyway.
22
--> $DIR/pats_with_wild_match_arm.rs:10:9
33
|
44
LL | "bar" | _ => {
5-
| ^^^^^^^^^ help: try this: `_`
5+
| ^^^^^^^^^ help: consider replacing with wildcard pattern only: `_`
66
|
77
= note: `-D clippy::pats-with-wild-match-arm` implied by `-D warnings`
88

9-
error: wildcard pattern covers any other pattern as it will match anyway. Consider replacing with wildcard pattern only
9+
error: wildcard pattern covers any other pattern as it will match anyway.
1010
--> $DIR/pats_with_wild_match_arm.rs:18:9
1111
|
1212
LL | "bar" | "bar2" | _ => {
13-
| ^^^^^^^^^^^^^^^^^^ help: try this: `_`
13+
| ^^^^^^^^^^^^^^^^^^ help: consider replacing with wildcard pattern only: `_`
1414

15-
error: wildcard pattern covers any other pattern as it will match anyway. Consider replacing with wildcard pattern only
15+
error: wildcard pattern covers any other pattern as it will match anyway.
1616
--> $DIR/pats_with_wild_match_arm.rs:26:9
1717
|
1818
LL | _ | "bar" | _ => {
19-
| ^^^^^^^^^^^^^ help: try this: `_`
19+
| ^^^^^^^^^^^^^ help: consider replacing with wildcard pattern only: `_`
2020

21-
error: wildcard pattern covers any other pattern as it will match anyway. Consider replacing with wildcard pattern only
21+
error: wildcard pattern covers any other pattern as it will match anyway.
2222
--> $DIR/pats_with_wild_match_arm.rs:34:9
2323
|
2424
LL | _ | "bar" => {
25-
| ^^^^^^^^^ help: try this: `_`
25+
| ^^^^^^^^^ help: consider replacing with wildcard pattern only: `_`
2626

2727
error: aborting due to 4 previous errors
2828

0 commit comments

Comments
 (0)