Pull the is_external test out of the loop

This commit is contained in:
Tyler Weaver 2023-01-30 17:29:17 -07:00
parent c531b09eb8
commit df7cdf732d
No known key found for this signature in database

View File

@ -3,7 +3,6 @@
use clippy_utils::{is_refutable, peel_hir_pat_refs, recurse_or_patterns};
use rustc_errors::Applicability;
use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::{Arm, Expr, PatKind, PathSegment, QPath, Ty, TyKind};
use rustc_lint::LateContext;
use rustc_middle::ty::{self, VariantDef};
@ -46,11 +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_external = adt_def.variants().iter().any(|x| is_external_and_hidden(cx, x));
let is_external = adt_def.did().as_local().is_none();
let has_external_hidden = is_external && adt_def.variants().iter().any(|x| is_hidden(cx, x));
let mut missing_variants: Vec<_> = adt_def
.variants()
.iter()
.filter(|x| !is_external_and_hidden(cx, x))
.filter(|x| !(is_external && is_hidden(cx, x)))
.collect();
let mut path_prefix = CommonPrefixSearcher::None;
@ -138,7 +138,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_external => span_lint_and_sugg(
[x] if !adt_def.is_variant_list_non_exhaustive() && !has_external_hidden => span_lint_and_sugg(
cx,
MATCH_WILDCARD_FOR_SINGLE_VARIANTS,
wildcard_span,
@ -149,7 +149,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_external {
let message = if adt_def.is_variant_list_non_exhaustive() || has_external_hidden {
suggestions.push("_".into());
"wildcard matches known variants and will also match future added variants"
} else {
@ -196,14 +196,6 @@ fn with_prefix(&mut self, path: &'a [PathSegment<'a>]) {
}
}
fn is_external_and_hidden(cx: &LateContext<'_>, variant_def: &VariantDef) -> bool {
is_external(variant_def.def_id) && is_hidden(cx, variant_def)
}
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_external(def_id: DefId) -> bool {
def_id.as_local().is_none()
}