@@ -267,7 +267,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Matches {
267267 check_wild_err_arm ( cx, ex, arms) ;
268268 check_wild_enum_match ( cx, ex, arms) ;
269269 check_match_as_ref ( cx, ex, arms, expr) ;
270- check_pats_wild_match ( cx, arms) ;
270+ check_pats_wild_match ( cx, ex , arms) ;
271271 }
272272 if let ExprKind :: Match ( ref ex, ref arms, _) = expr. kind {
273273 check_match_ref_pats ( cx, ex, arms, expr) ;
@@ -686,20 +686,45 @@ fn check_match_as_ref(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_>],
686686 }
687687}
688688
689- fn check_pats_wild_match ( cx : & LateContext < ' _ , ' _ > , arms : & [ Arm ] ) {
689+ fn check_pats_wild_match ( cx : & LateContext < ' _ , ' _ > , ex : & Expr < ' _ > , arms : & [ Arm < ' _ > ] ) {
690+ let mut is_non_exhaustive_enum = false ;
691+ let ty = cx. tables . expr_ty ( ex) ;
692+ if ty. is_enum ( ) {
693+ if let ty:: Adt ( def, _) = ty. kind {
694+ if def. is_variant_list_non_exhaustive ( ) {
695+ is_non_exhaustive_enum = true ;
696+ }
697+ }
698+ }
699+
690700 for arm in arms {
691701 if let PatKind :: Or ( ref fields) = arm. pat . kind {
692- // look for multiple fields where one at least matches Wild pattern
693- if fields. len ( ) > 1 && fields. into_iter ( ) . any ( is_wild) {
694- span_lint_and_sugg (
702+ // look for multiple fields in this arm that contains at least one Wild pattern
703+ if fields. len ( ) > 1 && fields. iter ( ) . any ( is_wild) {
704+ span_lint_and_then (
695705 cx,
696706 PATS_WITH_WILD_MATCH_ARM ,
697707 arm. pat . span ,
698- "wildcard pattern covers any other pattern as it will match anyway. Consider replacing with wildcard pattern only" ,
699- "try this" ,
700- "_" . to_string ( ) ,
701- Applicability :: MachineApplicable ,
702- )
708+ "wildcard pattern covers any other pattern as it will match anyway." ,
709+ |db| {
710+ // handle case where a non exhaustive enum is being used
711+ if is_non_exhaustive_enum {
712+ db. span_suggestion (
713+ arm. pat . span ,
714+ "consider handling `_` separately." ,
715+ "_ => ..." . to_string ( ) ,
716+ Applicability :: MaybeIncorrect ,
717+ ) ;
718+ } else {
719+ db. span_suggestion (
720+ arm. pat . span ,
721+ "consider replacing with wildcard pattern only" ,
722+ "_" . to_string ( ) ,
723+ Applicability :: MachineApplicable ,
724+ ) ;
725+ }
726+ } ,
727+ ) ;
703728 }
704729 }
705730 }
0 commit comments