Skip to content

Commit df7cdf7

Browse files
committed
Pull the is_external test out of the loop
1 parent c531b09 commit df7cdf7

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

clippy_lints/src/matches/match_wild_enum.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use clippy_utils::ty::is_type_diagnostic_item;
33
use clippy_utils::{is_refutable, peel_hir_pat_refs, recurse_or_patterns};
44
use rustc_errors::Applicability;
55
use rustc_hir::def::{CtorKind, DefKind, Res};
6-
use rustc_hir::def_id::DefId;
76
use rustc_hir::{Arm, Expr, PatKind, PathSegment, QPath, Ty, TyKind};
87
use rustc_lint::LateContext;
98
use rustc_middle::ty::{self, VariantDef};
@@ -46,11 +45,12 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
4645

4746
// Accumulate the variants which should be put in place of the wildcard because they're not
4847
// already covered.
49-
let has_hidden_external = adt_def.variants().iter().any(|x| is_external_and_hidden(cx, x));
48+
let is_external = adt_def.did().as_local().is_none();
49+
let has_external_hidden = is_external && adt_def.variants().iter().any(|x| is_hidden(cx, x));
5050
let mut missing_variants: Vec<_> = adt_def
5151
.variants()
5252
.iter()
53-
.filter(|x| !is_external_and_hidden(cx, x))
53+
.filter(|x| !(is_external && is_hidden(cx, x)))
5454
.collect();
5555

5656
let mut path_prefix = CommonPrefixSearcher::None;
@@ -138,7 +138,7 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
138138

139139
match missing_variants.as_slice() {
140140
[] => (),
141-
[x] if !adt_def.is_variant_list_non_exhaustive() && !has_hidden_external => span_lint_and_sugg(
141+
[x] if !adt_def.is_variant_list_non_exhaustive() && !has_external_hidden => span_lint_and_sugg(
142142
cx,
143143
MATCH_WILDCARD_FOR_SINGLE_VARIANTS,
144144
wildcard_span,
@@ -149,7 +149,7 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
149149
),
150150
variants => {
151151
let mut suggestions: Vec<_> = variants.iter().copied().map(format_suggestion).collect();
152-
let message = if adt_def.is_variant_list_non_exhaustive() || has_hidden_external {
152+
let message = if adt_def.is_variant_list_non_exhaustive() || has_external_hidden {
153153
suggestions.push("_".into());
154154
"wildcard matches known variants and will also match future added variants"
155155
} else {
@@ -196,14 +196,6 @@ impl<'a> CommonPrefixSearcher<'a> {
196196
}
197197
}
198198

199-
fn is_external_and_hidden(cx: &LateContext<'_>, variant_def: &VariantDef) -> bool {
200-
is_external(variant_def.def_id) && is_hidden(cx, variant_def)
201-
}
202-
203199
fn is_hidden(cx: &LateContext<'_>, variant_def: &VariantDef) -> bool {
204200
cx.tcx.is_doc_hidden(variant_def.def_id) || cx.tcx.has_attr(variant_def.def_id, sym::unstable)
205201
}
206-
207-
fn is_external(def_id: DefId) -> bool {
208-
def_id.as_local().is_none()
209-
}

0 commit comments

Comments
 (0)