Skip to content

Commit 2620d24

Browse files
committed
Fix check for missing enum variants from match expressions
TupleStruct matches are checked for exhaustiveness
1 parent 10313a2 commit 2620d24

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

clippy_lints/src/matches.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -764,9 +764,21 @@ fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_
764764
if let QPath::Resolved(_, p) = path {
765765
missing_variants.retain(|e| e.ctor_def_id != Some(p.res.def_id()));
766766
}
767-
} else if let PatKind::TupleStruct(ref path, ..) = arm.pat.kind {
767+
} else if let PatKind::TupleStruct(ref path, ref patterns, ..) = arm.pat.kind {
768768
if let QPath::Resolved(_, p) = path {
769-
missing_variants.retain(|e| e.ctor_def_id != Some(p.res.def_id()));
769+
// Some simple checks for exhaustive patterns.
770+
// There is a room for improvements to detect more cases,
771+
// but it can be more expensive to do so.
772+
let is_pattern_exhaustive = |pat: &&Pat<'_>| {
773+
if let PatKind::Wild | PatKind::Binding(.., None) = pat.kind {
774+
true
775+
} else {
776+
false
777+
}
778+
};
779+
if patterns.iter().all(is_pattern_exhaustive) {
780+
missing_variants.retain(|e| e.ctor_def_id != Some(p.res.def_id()));
781+
}
770782
}
771783
}
772784
}

clippy_lints/src/utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ pub fn trait_ref_of_method<'tcx>(cx: &LateContext<'_, 'tcx>, hir_id: HirId) -> O
372372
pub fn has_drop<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
373373
match ty.ty_adt_def() {
374374
Some(def) => def.has_dtor(cx.tcx),
375-
_ => false,
375+
None => false,
376376
}
377377
}
378378

0 commit comments

Comments
 (0)