Skip to content

Commit

Permalink
match_wildcard_for_single_variants: don't produce bad suggestion
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
flip1995 committed Jul 1, 2021
1 parent 0ffba7a commit fae7a09
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
5 changes: 3 additions & 2 deletions clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/match_wildcard_for_single_variants.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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 => (),
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/match_wildcard_for_single_variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => (),
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/wildcard_enum_match_arm.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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 | _ => (),
}
}
}
14 changes: 14 additions & 0 deletions tests/ui/wildcard_enum_match_arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => (),
_ => (),
}
}
}
8 changes: 7 additions & 1 deletion tests/ui/wildcard_enum_match_arm.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fae7a09

Please sign in to comment.