@@ -92,7 +92,10 @@ impl<'a, 'tcx> Visitor<'tcx> for MatchVisitor<'a, 'tcx> {
92
92
fn visit_local ( & mut self , loc : & ' tcx hir:: Local ) {
93
93
intravisit:: walk_local ( self , loc) ;
94
94
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
+ } ) ;
96
99
97
100
// Check legality of move bindings and `@` patterns.
98
101
self . check_patterns ( false , slice:: ref_slice ( & loc. pat ) ) ;
@@ -102,7 +105,7 @@ impl<'a, 'tcx> Visitor<'tcx> for MatchVisitor<'a, 'tcx> {
102
105
intravisit:: walk_body ( self , body) ;
103
106
104
107
for arg in & body. arguments {
105
- self . check_irrefutable ( & arg. pat , true ) ;
108
+ self . check_irrefutable ( & arg. pat , "function argument" ) ;
106
109
self . check_patterns ( false , slice:: ref_slice ( & arg. pat ) ) ;
107
110
}
108
111
}
@@ -211,7 +214,7 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
211
214
. map ( |pat| vec ! [ pat. 0 ] )
212
215
. collect ( ) ;
213
216
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) ;
215
218
} )
216
219
}
217
220
@@ -224,13 +227,7 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
224
227
}
225
228
}
226
229
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 ) {
234
231
let module = self . tcx . hir . get_module_parent ( pat. id ) ;
235
232
MatchCheckCtxt :: create_and_enter ( self . tcx , module, |ref mut cx| {
236
233
let mut patcx = PatternContext :: new ( self . tcx , self . tables ) ;
@@ -396,8 +393,7 @@ fn check_arms<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
396
393
fn check_exhaustive < ' a , ' tcx > ( cx : & mut MatchCheckCtxt < ' a , ' tcx > ,
397
394
scrut_ty : Ty < ' tcx > ,
398
395
sp : Span ,
399
- matrix : & Matrix < ' a , ' tcx > ,
400
- source : hir:: MatchSource ) {
396
+ matrix : & Matrix < ' a , ' tcx > ) {
401
397
let wild_pattern = Pattern {
402
398
ty : scrut_ty,
403
399
span : DUMMY_SP ,
@@ -410,52 +406,32 @@ fn check_exhaustive<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
410
406
} else {
411
407
pats. iter ( ) . map ( |w| w. single_pattern ( ) ) . collect ( )
412
408
} ;
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)
430
418
} ,
431
419
_ => {
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 ( ) ;
459
435
}
460
436
NotUseful => {
461
437
// This is good, wildcard pattern isn't reachable
0 commit comments