@@ -366,14 +366,31 @@ fn unreachable_pattern(tcx: TyCtxt<'_>, span: Span, id: HirId, catchall: Option<
366
366
}
367
367
368
368
fn irrefutable_let_pattern ( tcx : TyCtxt < ' _ > , span : Span , id : HirId , source : hir:: MatchSource ) {
369
- tcx. struct_span_lint_hir ( IRREFUTABLE_LET_PATTERNS , id, span, |lint| {
370
- let msg = match source {
371
- hir:: MatchSource :: IfLetDesugar { .. } => "irrefutable `if let` pattern" ,
372
- hir:: MatchSource :: WhileLetDesugar => "irrefutable `while let` pattern" ,
373
- hir:: MatchSource :: IfLetGuardDesugar => "irrefutable `if let` guard" ,
374
- _ => bug ! ( ) ,
375
- } ;
376
- lint. build ( msg) . emit ( )
369
+ tcx. struct_span_lint_hir ( IRREFUTABLE_LET_PATTERNS , id, span, |lint| match source {
370
+ hir:: MatchSource :: IfLetDesugar { .. } => {
371
+ let mut diag = lint. build ( "irrefutable `if let` pattern" ) ;
372
+ diag. note ( "this pattern will always match, so the `if let` is useless" ) ;
373
+ diag. help ( "consider replacing the `if let` with a `let`" ) ;
374
+ diag. emit ( )
375
+ }
376
+ hir:: MatchSource :: WhileLetDesugar => {
377
+ let mut diag = lint. build ( "irrefutable `while let` pattern" ) ;
378
+ diag. note ( "this pattern will always match, so the loop will never exit" ) ;
379
+ diag. help ( "consider instead using a `loop { ... }` with a `let` inside it" ) ;
380
+ diag. emit ( )
381
+ }
382
+ hir:: MatchSource :: IfLetGuardDesugar => {
383
+ let mut diag = lint. build ( "irrefutable `if let` guard pattern" ) ;
384
+ diag. note ( "this pattern will always match, so the guard is useless" ) ;
385
+ diag. help ( "consider removing the guard and adding a `let` inside the match arm" ) ;
386
+ diag. emit ( )
387
+ }
388
+ _ => {
389
+ bug ! (
390
+ "expected `if let`, `while let`, or `if let` guard HIR match source, found {:?}" ,
391
+ source,
392
+ )
393
+ }
377
394
} ) ;
378
395
}
379
396
@@ -387,7 +404,7 @@ fn check_if_let_guard<'p, 'tcx>(
387
404
report_arm_reachability ( & cx, & report, hir:: MatchSource :: IfLetGuardDesugar ) ;
388
405
389
406
if report. non_exhaustiveness_witnesses . is_empty ( ) {
390
- // The match is exhaustive, i.e. the if let pattern is irrefutable.
407
+ // The match is exhaustive, i.e. the ` if let` pattern is irrefutable.
391
408
irrefutable_let_pattern ( cx. tcx , pat. span , pat_id, hir:: MatchSource :: IfLetGuardDesugar )
392
409
}
393
410
}
0 commit comments