@@ -92,7 +92,10 @@ impl<'a, 'tcx> Visitor<'tcx> for MatchVisitor<'a, 'tcx> {
9292 fn visit_local ( & mut self , loc : & ' tcx hir:: Local ) {
9393 intravisit:: walk_local ( self , loc) ;
9494
95- self . check_irrefutable ( & loc. pat , false ) ;
95+ self . check_irrefutable ( & loc. pat , match loc. source {
96+ hir:: LocalSource :: Normal => "local binding" ,
97+ hir:: LocalSource :: ForLoopDesugar => "`for` loop binding" ,
98+ } ) ;
9699
97100 // Check legality of move bindings and `@` patterns.
98101 self . check_patterns ( false , slice:: ref_slice ( & loc. pat ) ) ;
@@ -102,7 +105,7 @@ impl<'a, 'tcx> Visitor<'tcx> for MatchVisitor<'a, 'tcx> {
102105 intravisit:: walk_body ( self , body) ;
103106
104107 for arg in & body. arguments {
105- self . check_irrefutable ( & arg. pat , true ) ;
108+ self . check_irrefutable ( & arg. pat , "function argument" ) ;
106109 self . check_patterns ( false , slice:: ref_slice ( & arg. pat ) ) ;
107110 }
108111 }
@@ -211,7 +214,7 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
211214 . map ( |pat| vec ! [ pat. 0 ] )
212215 . collect ( ) ;
213216 let scrut_ty = self . tables . node_id_to_type ( scrut. id ) ;
214- check_exhaustive ( cx, scrut_ty, scrut. span , & matrix, source ) ;
217+ check_exhaustive ( cx, scrut_ty, scrut. span , & matrix) ;
215218 } )
216219 }
217220
@@ -224,13 +227,7 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
224227 }
225228 }
226229
227- fn check_irrefutable ( & self , pat : & Pat , is_fn_arg : bool ) {
228- let origin = if is_fn_arg {
229- "function argument"
230- } else {
231- "local binding"
232- } ;
233-
230+ fn check_irrefutable ( & self , pat : & Pat , origin : & str ) {
234231 let module = self . tcx . hir . get_module_parent ( pat. id ) ;
235232 MatchCheckCtxt :: create_and_enter ( self . tcx , module, |ref mut cx| {
236233 let mut patcx = PatternContext :: new ( self . tcx , self . tables ) ;
@@ -396,8 +393,7 @@ fn check_arms<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
396393fn check_exhaustive < ' a , ' tcx > ( cx : & mut MatchCheckCtxt < ' a , ' tcx > ,
397394 scrut_ty : Ty < ' tcx > ,
398395 sp : Span ,
399- matrix : & Matrix < ' a , ' tcx > ,
400- source : hir:: MatchSource ) {
396+ matrix : & Matrix < ' a , ' tcx > ) {
401397 let wild_pattern = Pattern {
402398 ty : scrut_ty,
403399 span : DUMMY_SP ,
@@ -410,52 +406,32 @@ fn check_exhaustive<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
410406 } else {
411407 pats. iter ( ) . map ( |w| w. single_pattern ( ) ) . collect ( )
412408 } ;
413- match source {
414- hir:: MatchSource :: ForLoopDesugar => {
415- // `witnesses[0]` has the form `Some(<head>)`, peel off the `Some`
416- let witness = match * witnesses[ 0 ] . kind {
417- PatternKind :: Variant { ref subpatterns, .. } => match & subpatterns[ ..] {
418- & [ ref pat] => & pat. pattern ,
419- _ => bug ! ( ) ,
420- } ,
421- _ => bug ! ( ) ,
422- } ;
423- let pattern_string = witness. to_string ( ) ;
424- struct_span_err ! ( cx. tcx. sess, sp, E0297 ,
425- "refutable pattern in `for` loop binding: \
426- `{}` not covered",
427- pattern_string)
428- . span_label ( sp, format ! ( "pattern `{}` not covered" , pattern_string) )
429- . emit ( ) ;
409+
410+ const LIMIT : usize = 3 ;
411+ let joined_patterns = match witnesses. len ( ) {
412+ 0 => bug ! ( ) ,
413+ 1 => format ! ( "`{}`" , witnesses[ 0 ] ) ,
414+ 2 ...LIMIT => {
415+ let ( tail, head) = witnesses. split_last ( ) . unwrap ( ) ;
416+ let head: Vec < _ > = head. iter ( ) . map ( |w| w. to_string ( ) ) . collect ( ) ;
417+ format ! ( "`{}` and `{}`" , head. join( "`, `" ) , tail)
430418 } ,
431419 _ => {
432- const LIMIT : usize = 3 ;
433- let joined_patterns = match witnesses. len ( ) {
434- 0 => bug ! ( ) ,
435- 1 => format ! ( "`{}`" , witnesses[ 0 ] ) ,
436- 2 ...LIMIT => {
437- let ( tail, head) = witnesses. split_last ( ) . unwrap ( ) ;
438- let head: Vec < _ > = head. iter ( ) . map ( |w| w. to_string ( ) ) . collect ( ) ;
439- format ! ( "`{}` and `{}`" , head. join( "`, `" ) , tail)
440- } ,
441- _ => {
442- let ( head, tail) = witnesses. split_at ( LIMIT ) ;
443- let head: Vec < _ > = head. iter ( ) . map ( |w| w. to_string ( ) ) . collect ( ) ;
444- format ! ( "`{}` and {} more" , head. join( "`, `" ) , tail. len( ) )
445- }
446- } ;
447-
448- let label_text = match witnesses. len ( ) {
449- 1 => format ! ( "pattern {} not covered" , joined_patterns) ,
450- _ => format ! ( "patterns {} not covered" , joined_patterns)
451- } ;
452- create_e0004 ( cx. tcx . sess , sp,
453- format ! ( "non-exhaustive patterns: {} not covered" ,
454- joined_patterns) )
455- . span_label ( sp, label_text)
456- . emit ( ) ;
457- } ,
458- }
420+ let ( head, tail) = witnesses. split_at ( LIMIT ) ;
421+ let head: Vec < _ > = head. iter ( ) . map ( |w| w. to_string ( ) ) . collect ( ) ;
422+ format ! ( "`{}` and {} more" , head. join( "`, `" ) , tail. len( ) )
423+ }
424+ } ;
425+
426+ let label_text = match witnesses. len ( ) {
427+ 1 => format ! ( "pattern {} not covered" , joined_patterns) ,
428+ _ => format ! ( "patterns {} not covered" , joined_patterns)
429+ } ;
430+ create_e0004 ( cx. tcx . sess , sp,
431+ format ! ( "non-exhaustive patterns: {} not covered" ,
432+ joined_patterns) )
433+ . span_label ( sp, label_text)
434+ . emit ( ) ;
459435 }
460436 NotUseful => {
461437 // This is good, wildcard pattern isn't reachable
0 commit comments