@@ -688,13 +688,14 @@ impl<'p, 'tcx> WitnessMatrix<'p, 'tcx> {
688
688
pcx : & PatCtxt < ' _ , ' p , ' tcx > ,
689
689
missing_ctors : & [ Constructor < ' tcx > ] ,
690
690
ctor : & Constructor < ' tcx > ,
691
+ report_when_all_missing : bool ,
691
692
) {
692
693
if self . is_empty ( ) {
693
694
return ;
694
695
}
695
- if matches ! ( ctor, Constructor :: Wildcard ) {
696
+ if matches ! ( ctor, Constructor :: Wildcard ) && !report_when_all_missing {
696
697
self . push_wild_ctor ( pcx, Constructor :: Wildcard ) ;
697
- } else if matches ! ( ctor, Constructor :: Missing { .. } ) {
698
+ } else if matches ! ( ctor, Constructor :: Wildcard | Constructor :: Missing ) {
698
699
if missing_ctors. iter ( ) . any ( |c| c. is_non_exhaustive ( ) ) {
699
700
// Here we don't want the user to try to list all variants, we want them to add a
700
701
// wildcard, so we only suggest that.
@@ -787,6 +788,9 @@ fn compute_usefulness<'p, 'tcx>(
787
788
let ctor_set = ConstructorSet :: new ( pcx) ;
788
789
let ( split_ctors, missing_ctors) =
789
790
ctor_set. split ( pcx, matrix. heads ( ) . map ( |p| p. ctor ( ) ) , is_top_level) ;
791
+ // At the top level, we prefer to list all constructors when `_` could be reported as missing.
792
+ let report_when_all_missing =
793
+ is_top_level && !matches ! ( ctor_set, ConstructorSet :: Integers { .. } ) ;
790
794
791
795
// For each constructor, we compute whether there's a value that starts with it that would
792
796
// witness the usefulness of `v`.
@@ -797,7 +801,7 @@ fn compute_usefulness<'p, 'tcx>(
797
801
let mut spec_matrix = matrix. specialize_constructor ( pcx, & ctor) ;
798
802
let mut witnesses =
799
803
ensure_sufficient_stack ( || compute_usefulness ( cx, & mut spec_matrix, lint_root, false ) ) ;
800
- witnesses. apply_constructor ( pcx, & missing_ctors, & ctor) ;
804
+ witnesses. apply_constructor ( pcx, & missing_ctors, & ctor, report_when_all_missing ) ;
801
805
ret. extend ( witnesses) ;
802
806
803
807
// Lint on likely incorrect range patterns (#63987)
@@ -827,7 +831,8 @@ fn compute_usefulness<'p, 'tcx>(
827
831
// When all the conditions are met we have a match with a `non_exhaustive` enum
828
832
// that has the potential to trigger the `non_exhaustive_omitted_patterns` lint.
829
833
if cx. refutable
830
- && matches ! ( & ctor, Constructor :: Missing )
834
+ && ( matches ! ( & ctor, Constructor :: Missing )
835
+ || ( matches ! ( & ctor, Constructor :: Wildcard ) && is_top_level) )
831
836
&& matches ! ( & ctor_set, ConstructorSet :: Variants { non_exhaustive: true , .. } )
832
837
&& spec_matrix. rows ( ) . len ( ) != 0
833
838
{
0 commit comments