Skip to content

Commit fae7a09

Browse files
committed
match_wildcard_for_single_variants: don't produce bad suggestion
This fixes a bug where match_wildcard_for_single_variants produced a bad suggestion where besides the missing variant, one or more hidden variants were left. This also adds tests to the ui-tests match_wildcard_for_single_variants and wildcard_enum_match_arm to make sure that the correct suggestion is produced.
1 parent 0ffba7a commit fae7a09

6 files changed

+52
-3
lines changed

clippy_lints/src/matches.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,7 @@ fn check_wild_enum_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>])
10331033

10341034
// Accumulate the variants which should be put in place of the wildcard because they're not
10351035
// already covered.
1036+
let has_hidden = adt_def.variants.iter().any(|x| is_hidden(cx, x));
10361037
let mut missing_variants: Vec<_> = adt_def.variants.iter().filter(|x| !is_hidden(cx, x)).collect();
10371038

10381039
let mut path_prefix = CommonPrefixSearcher::None;
@@ -1118,7 +1119,7 @@ fn check_wild_enum_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>])
11181119

11191120
match missing_variants.as_slice() {
11201121
[] => (),
1121-
[x] if !adt_def.is_variant_list_non_exhaustive() => span_lint_and_sugg(
1122+
[x] if !adt_def.is_variant_list_non_exhaustive() && !has_hidden => span_lint_and_sugg(
11221123
cx,
11231124
MATCH_WILDCARD_FOR_SINGLE_VARIANTS,
11241125
wildcard_span,
@@ -1129,7 +1130,7 @@ fn check_wild_enum_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>])
11291130
),
11301131
variants => {
11311132
let mut suggestions: Vec<_> = variants.iter().copied().map(format_suggestion).collect();
1132-
let message = if adt_def.is_variant_list_non_exhaustive() {
1133+
let message = if adt_def.is_variant_list_non_exhaustive() || has_hidden {
11331134
suggestions.push("_".into());
11341135
"wildcard matches known variants and will also match future added variants"
11351136
} else {

tests/ui/match_wildcard_for_single_variants.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,16 @@ fn main() {
115115
pub enum Enum {
116116
A,
117117
B,
118+
C,
118119
#[doc(hidden)]
119120
__Private,
120121
}
122+
match Enum::A {
123+
Enum::A => (),
124+
Enum::B => (),
125+
Enum::C => (),
126+
_ => (),
127+
}
121128
match Enum::A {
122129
Enum::A => (),
123130
Enum::B => (),

tests/ui/match_wildcard_for_single_variants.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,16 @@ fn main() {
115115
pub enum Enum {
116116
A,
117117
B,
118+
C,
118119
#[doc(hidden)]
119120
__Private,
120121
}
122+
match Enum::A {
123+
Enum::A => (),
124+
Enum::B => (),
125+
Enum::C => (),
126+
_ => (),
127+
}
121128
match Enum::A {
122129
Enum::A => (),
123130
Enum::B => (),

tests/ui/wildcard_enum_match_arm.fixed

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,18 @@ fn main() {
8787
ErrorKind::PermissionDenied => {},
8888
_ => {},
8989
}
90+
91+
{
92+
#![allow(clippy::manual_non_exhaustive)]
93+
pub enum Enum {
94+
A,
95+
B,
96+
#[doc(hidden)]
97+
__Private,
98+
}
99+
match Enum::A {
100+
Enum::A => (),
101+
Enum::B | _ => (),
102+
}
103+
}
90104
}

tests/ui/wildcard_enum_match_arm.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,18 @@ fn main() {
8787
ErrorKind::PermissionDenied => {},
8888
_ => {},
8989
}
90+
91+
{
92+
#![allow(clippy::manual_non_exhaustive)]
93+
pub enum Enum {
94+
A,
95+
B,
96+
#[doc(hidden)]
97+
__Private,
98+
}
99+
match Enum::A {
100+
Enum::A => (),
101+
_ => (),
102+
}
103+
}
90104
}

tests/ui/wildcard_enum_match_arm.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,11 @@ error: wildcard matches known variants and will also match future added variants
3434
LL | _ => {},
3535
| ^ help: try this: `ErrorKind::PermissionDenied | _`
3636

37-
error: aborting due to 5 previous errors
37+
error: wildcard matches known variants and will also match future added variants
38+
--> $DIR/wildcard_enum_match_arm.rs:101:13
39+
|
40+
LL | _ => (),
41+
| ^ help: try this: `Enum::B | _`
42+
43+
error: aborting due to 6 previous errors
3844

0 commit comments

Comments
 (0)