@@ -413,7 +413,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
413
413
// Emit lints in the order in which they occur in the file.
414
414
redundant_subpats. sort_unstable_by_key ( |( pat, _) | pat. data ( ) . span ) ;
415
415
for ( pat, explanation) in redundant_subpats {
416
- report_unreachable_pattern ( cx, arm. arm_data , pat, & explanation)
416
+ report_unreachable_pattern ( cx, arm. arm_data , pat, & explanation, None )
417
417
}
418
418
}
419
419
}
@@ -474,7 +474,11 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
474
474
hir:: MatchSource :: ForLoopDesugar
475
475
| hir:: MatchSource :: Postfix
476
476
| hir:: MatchSource :: Normal
477
- | hir:: MatchSource :: FormatArgs => report_arm_reachability ( & cx, & report) ,
477
+ | hir:: MatchSource :: FormatArgs => {
478
+ let is_match_arm =
479
+ matches ! ( source, hir:: MatchSource :: Postfix | hir:: MatchSource :: Normal ) ;
480
+ report_arm_reachability ( & cx, & report, is_match_arm) ;
481
+ }
478
482
// Unreachable patterns in try and await expressions occur when one of
479
483
// the arms are an uninhabited type. Which is OK.
480
484
hir:: MatchSource :: AwaitDesugar | hir:: MatchSource :: TryDesugar ( _) => { }
@@ -626,7 +630,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
626
630
) -> Result < RefutableFlag , ErrorGuaranteed > {
627
631
let ( cx, report) = self . analyze_binding ( pat, Refutable , scrut) ?;
628
632
// Report if the pattern is unreachable, which can only occur when the type is uninhabited.
629
- report_arm_reachability ( & cx, & report) ;
633
+ report_arm_reachability ( & cx, & report, false ) ;
630
634
// If the list of witnesses is empty, the match is exhaustive, i.e. the `if let` pattern is
631
635
// irrefutable.
632
636
Ok ( if report. non_exhaustiveness_witnesses . is_empty ( ) { Irrefutable } else { Refutable } )
@@ -916,6 +920,7 @@ fn report_unreachable_pattern<'p, 'tcx>(
916
920
hir_id : HirId ,
917
921
pat : & DeconstructedPat < ' p , ' tcx > ,
918
922
explanation : & RedundancyExplanation < ' p , ' tcx > ,
923
+ whole_arm_span : Option < Span > ,
919
924
) {
920
925
static CAP_COVERED_BY_MANY : usize = 4 ;
921
926
let pat_span = pat. data ( ) . span ;
@@ -928,13 +933,15 @@ fn report_unreachable_pattern<'p, 'tcx>(
928
933
covered_by_one : None ,
929
934
covered_by_many : None ,
930
935
covered_by_many_n_more_count : 0 ,
936
+ suggest_remove : None ,
931
937
} ;
932
938
match explanation. covered_by . as_slice ( ) {
933
939
[ ] => {
934
940
// Empty pattern; we report the uninhabited type that caused the emptiness.
935
941
lint. span = None ; // Don't label the pattern itself
936
942
lint. uninhabited_note = Some ( ( ) ) ; // Give a link about empty types
937
943
lint. matches_no_values = Some ( pat_span) ;
944
+ lint. suggest_remove = whole_arm_span; // Suggest to remove the match arm
938
945
pat. walk ( & mut |subpat| {
939
946
let ty = * * subpat. ty ( ) ;
940
947
if cx. is_uninhabited ( ty) {
@@ -982,10 +989,28 @@ fn report_unreachable_pattern<'p, 'tcx>(
982
989
}
983
990
984
991
/// Report unreachable arms, if any.
985
- fn report_arm_reachability < ' p , ' tcx > ( cx : & PatCtxt < ' p , ' tcx > , report : & UsefulnessReport < ' p , ' tcx > ) {
992
+ fn report_arm_reachability < ' p , ' tcx > (
993
+ cx : & PatCtxt < ' p , ' tcx > ,
994
+ report : & UsefulnessReport < ' p , ' tcx > ,
995
+ is_match_arm : bool ,
996
+ ) {
997
+ let sm = cx. tcx . sess . source_map ( ) ;
986
998
for ( arm, is_useful) in report. arm_usefulness . iter ( ) {
987
999
if let Usefulness :: Redundant ( explanation) = is_useful {
988
- report_unreachable_pattern ( cx, arm. arm_data , arm. pat , explanation)
1000
+ let hir_id = arm. arm_data ;
1001
+ let arm_span = cx. tcx . hir ( ) . span ( hir_id) ;
1002
+ let whole_arm_span = if is_match_arm {
1003
+ // If the arm is followed by a comma, extend the span to include it.
1004
+ let with_whitespace = sm. span_extend_while_whitespace ( arm_span) ;
1005
+ if let Some ( comma) = sm. span_look_ahead ( with_whitespace, "," , Some ( 1 ) ) {
1006
+ Some ( arm_span. to ( comma) )
1007
+ } else {
1008
+ Some ( arm_span)
1009
+ }
1010
+ } else {
1011
+ None
1012
+ } ;
1013
+ report_unreachable_pattern ( cx, hir_id, arm. pat , explanation, whole_arm_span)
989
1014
}
990
1015
}
991
1016
}
0 commit comments