@@ -79,6 +79,8 @@ enum LetSource {
7979 IfLetGuard ,
8080 LetElse ,
8181 WhileLet ,
82+ Else ,
83+ ElseIfLet ,
8284}
8385
8486struct MatchVisitor < ' p , ' tcx > {
@@ -129,15 +131,20 @@ impl<'p, 'tcx> Visitor<'p, 'tcx> for MatchVisitor<'p, 'tcx> {
129131 // Give a specific `let_source` for the condition.
130132 let let_source = match ex. span . desugaring_kind ( ) {
131133 Some ( DesugaringKind :: WhileLoop ) => LetSource :: WhileLet ,
132- _ => LetSource :: IfLet ,
134+ _ => match self . let_source {
135+ LetSource :: Else => LetSource :: ElseIfLet ,
136+ _ => LetSource :: IfLet ,
137+ } ,
133138 } ;
134139 self . with_let_source ( let_source, |this| this. visit_expr ( & self . thir [ cond] ) ) ;
135140 self . with_let_source ( LetSource :: None , |this| {
136141 this. visit_expr ( & this. thir [ then] ) ;
137- if let Some ( else_) = else_opt {
138- this. visit_expr ( & this. thir [ else_] ) ;
139- }
140142 } ) ;
143+ if let Some ( else_) = else_opt {
144+ self . with_let_source ( LetSource :: Else , |this| {
145+ this. visit_expr ( & this. thir [ else_] )
146+ } ) ;
147+ }
141148 return ;
142149 }
143150 ExprKind :: Match { scrutinee, scrutinee_hir_id : _, box ref arms, match_source } => {
@@ -569,9 +576,13 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
569576 // and we shouldn't lint.
570577 // For let guards inside a match, prefixes might use bindings of the match pattern,
571578 // so can't always be moved out.
579+ // For `else if let`, an extra indentation level would be required to move the bindings.
572580 // FIXME: Add checking whether the bindings are actually used in the prefix,
573581 // and lint if they are not.
574- if !matches ! ( self . let_source, LetSource :: WhileLet | LetSource :: IfLetGuard ) {
582+ if !matches ! (
583+ self . let_source,
584+ LetSource :: WhileLet | LetSource :: IfLetGuard | LetSource :: ElseIfLet
585+ ) {
575586 // Emit the lint
576587 let prefix = & chain_refutabilities[ ..until] ;
577588 let span_start = prefix[ 0 ] . unwrap ( ) . 0 ;
@@ -902,8 +913,8 @@ fn report_irrefutable_let_patterns(
902913 }
903914
904915 match source {
905- LetSource :: None | LetSource :: PlainLet => bug ! ( ) ,
906- LetSource :: IfLet => emit_diag ! ( IrrefutableLetPatternsIfLet ) ,
916+ LetSource :: None | LetSource :: PlainLet | LetSource :: Else => bug ! ( ) ,
917+ LetSource :: IfLet | LetSource :: ElseIfLet => emit_diag ! ( IrrefutableLetPatternsIfLet ) ,
907918 LetSource :: IfLetGuard => emit_diag ! ( IrrefutableLetPatternsIfLetGuard ) ,
908919 LetSource :: LetElse => emit_diag ! ( IrrefutableLetPatternsLetElse ) ,
909920 LetSource :: WhileLet => emit_diag ! ( IrrefutableLetPatternsWhileLet ) ,
0 commit comments