mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 21:23:56 +00:00
wildcard_enum_match_arm lint takes the enum origin into account
Signed-off-by: Tyler Weaver <maybe@tylerjw.dev>
This commit is contained in:
parent
d020fd7fe6
commit
2432e97d6a
5 changed files with 22 additions and 11 deletions
|
@ -45,8 +45,12 @@ pub(crate) fn check(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 has_hidden_external = adt_def.variants().iter().any(|x| is_hidden_and_external(cx, x));
|
||||
let mut missing_variants: Vec<_> = adt_def
|
||||
.variants()
|
||||
.iter()
|
||||
.filter(|x| !is_hidden_and_external(cx, x))
|
||||
.collect();
|
||||
|
||||
let mut path_prefix = CommonPrefixSearcher::None;
|
||||
for arm in arms {
|
||||
|
@ -133,7 +137,7 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
|
|||
|
||||
match missing_variants.as_slice() {
|
||||
[] => (),
|
||||
[x] if !adt_def.is_variant_list_non_exhaustive() && !has_hidden => span_lint_and_sugg(
|
||||
[x] if !adt_def.is_variant_list_non_exhaustive() && !has_hidden_external => span_lint_and_sugg(
|
||||
cx,
|
||||
MATCH_WILDCARD_FOR_SINGLE_VARIANTS,
|
||||
wildcard_span,
|
||||
|
@ -144,7 +148,7 @@ pub(crate) fn check(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() || has_hidden {
|
||||
let message = if adt_def.is_variant_list_non_exhaustive() || has_hidden_external {
|
||||
suggestions.push("_".into());
|
||||
"wildcard matches known variants and will also match future added variants"
|
||||
} else {
|
||||
|
@ -191,6 +195,7 @@ impl<'a> CommonPrefixSearcher<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn is_hidden(cx: &LateContext<'_>, variant_def: &VariantDef) -> bool {
|
||||
cx.tcx.is_doc_hidden(variant_def.def_id) || cx.tcx.has_attr(variant_def.def_id, sym::unstable)
|
||||
fn is_hidden_and_external(cx: &LateContext<'_>, variant_def: &VariantDef) -> bool {
|
||||
(cx.tcx.is_doc_hidden(variant_def.def_id) || cx.tcx.has_attr(variant_def.def_id, sym::unstable))
|
||||
&& variant_def.def_id.as_local().is_none()
|
||||
}
|
||||
|
|
|
@ -123,7 +123,7 @@ fn main() {
|
|||
Enum::A => (),
|
||||
Enum::B => (),
|
||||
Enum::C => (),
|
||||
_ => (),
|
||||
Enum::__Private => (),
|
||||
}
|
||||
match Enum::A {
|
||||
Enum::A => (),
|
||||
|
|
|
@ -48,11 +48,17 @@ error: wildcard matches only a single variant and will also match any future add
|
|||
LL | _ => (),
|
||||
| ^ help: try this: `Color::Blue`
|
||||
|
||||
error: wildcard matches only a single variant and will also match any future added variants
|
||||
--> $DIR/match_wildcard_for_single_variants.rs:126:13
|
||||
|
|
||||
LL | _ => (),
|
||||
| ^ help: try this: `Enum::__Private`
|
||||
|
||||
error: wildcard matches only a single variant and will also match any future added variants
|
||||
--> $DIR/match_wildcard_for_single_variants.rs:153:13
|
||||
|
|
||||
LL | _ => 2,
|
||||
| ^ help: try this: `Foo::B`
|
||||
|
||||
error: aborting due to 9 previous errors
|
||||
error: aborting due to 10 previous errors
|
||||
|
||||
|
|
|
@ -96,7 +96,7 @@ fn main() {
|
|||
}
|
||||
match Enum::A {
|
||||
Enum::A => (),
|
||||
Enum::B | _ => (),
|
||||
Enum::B | Enum::__Private => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,11 +34,11 @@ error: wildcard matches known variants and will also match future added variants
|
|||
LL | _ => {},
|
||||
| ^ help: try this: `ErrorKind::PermissionDenied | _`
|
||||
|
||||
error: wildcard matches known variants and will also match future added variants
|
||||
error: wildcard match will also match any future added variants
|
||||
--> $DIR/wildcard_enum_match_arm.rs:99:13
|
||||
|
|
||||
LL | _ => (),
|
||||
| ^ help: try this: `Enum::B | _`
|
||||
| ^ help: try this: `Enum::B | Enum::__Private`
|
||||
|
||||
error: aborting due to 6 previous errors
|
||||
|
||||
|
|
Loading…
Reference in a new issue