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.
This commit is contained in:
flip1995 2021-07-01 12:35:16 +02:00
parent 0ffba7a684
commit fae7a09eea
No known key found for this signature in database
GPG Key ID: 1CA0DF2AF59D68A5
6 changed files with 52 additions and 3 deletions

View File

@ -1033,6 +1033,7 @@ fn check_wild_enum_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>])
// Accumulate the variants which should be put in place of the wildcard because they're not
// already covered.
let has_hidden = adt_def.variants.iter().any(|x| is_hidden(cx, x));
let mut missing_variants: Vec<_> = adt_def.variants.iter().filter(|x| !is_hidden(cx, x)).collect();
let mut path_prefix = CommonPrefixSearcher::None;
@ -1118,7 +1119,7 @@ fn check_wild_enum_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>])
match missing_variants.as_slice() {
[] => (),
[x] if !adt_def.is_variant_list_non_exhaustive() => span_lint_and_sugg(
[x] if !adt_def.is_variant_list_non_exhaustive() && !has_hidden => span_lint_and_sugg(
cx,
MATCH_WILDCARD_FOR_SINGLE_VARIANTS,
wildcard_span,
@ -1129,7 +1130,7 @@ fn check_wild_enum_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>])
),
variants => {
let mut suggestions: Vec<_> = variants.iter().copied().map(format_suggestion).collect();
let message = if adt_def.is_variant_list_non_exhaustive() {
let message = if adt_def.is_variant_list_non_exhaustive() || has_hidden {
suggestions.push("_".into());
"wildcard matches known variants and will also match future added variants"
} else {

View File

@ -115,9 +115,16 @@ fn main() {
pub enum Enum {
A,
B,
C,
#[doc(hidden)]
__Private,
}
match Enum::A {
Enum::A => (),
Enum::B => (),
Enum::C => (),
_ => (),
}
match Enum::A {
Enum::A => (),
Enum::B => (),

View File

@ -115,9 +115,16 @@ fn main() {
pub enum Enum {
A,
B,
C,
#[doc(hidden)]
__Private,
}
match Enum::A {
Enum::A => (),
Enum::B => (),
Enum::C => (),
_ => (),
}
match Enum::A {
Enum::A => (),
Enum::B => (),

View File

@ -87,4 +87,18 @@ fn main() {
ErrorKind::PermissionDenied => {},
_ => {},
}
{
#![allow(clippy::manual_non_exhaustive)]
pub enum Enum {
A,
B,
#[doc(hidden)]
__Private,
}
match Enum::A {
Enum::A => (),
Enum::B | _ => (),
}
}
}

View File

@ -87,4 +87,18 @@ fn main() {
ErrorKind::PermissionDenied => {},
_ => {},
}
{
#![allow(clippy::manual_non_exhaustive)]
pub enum Enum {
A,
B,
#[doc(hidden)]
__Private,
}
match Enum::A {
Enum::A => (),
_ => (),
}
}
}

View File

@ -34,5 +34,11 @@ error: wildcard matches known variants and will also match future added variants
LL | _ => {},
| ^ help: try this: `ErrorKind::PermissionDenied | _`
error: aborting due to 5 previous errors
error: wildcard matches known variants and will also match future added variants
--> $DIR/wildcard_enum_match_arm.rs:101:13
|
LL | _ => (),
| ^ help: try this: `Enum::B | _`
error: aborting due to 6 previous errors