@@ -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 } => {
@@ -573,9 +580,13 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
573580 // and we shouldn't lint.
574581 // For let guards inside a match, prefixes might use bindings of the match pattern,
575582 // so can't always be moved out.
583+ // For `else if let`, an extra indentation level would be required to move the bindings.
576584 // FIXME: Add checking whether the bindings are actually used in the prefix,
577585 // and lint if they are not.
578- if !matches ! ( self . let_source, LetSource :: WhileLet | LetSource :: IfLetGuard ) {
586+ if !matches ! (
587+ self . let_source,
588+ LetSource :: WhileLet | LetSource :: IfLetGuard | LetSource :: ElseIfLet
589+ ) {
579590 // Emit the lint
580591 let prefix = & chain_refutabilities[ ..until] ;
581592 let span_start = prefix[ 0 ] . unwrap ( ) . 0 ;
@@ -906,8 +917,8 @@ fn report_irrefutable_let_patterns(
906917 }
907918
908919 match source {
909- LetSource :: None | LetSource :: PlainLet => bug ! ( ) ,
910- LetSource :: IfLet => emit_diag ! ( IrrefutableLetPatternsIfLet ) ,
920+ LetSource :: None | LetSource :: PlainLet | LetSource :: Else => bug ! ( ) ,
921+ LetSource :: IfLet | LetSource :: ElseIfLet => emit_diag ! ( IrrefutableLetPatternsIfLet ) ,
911922 LetSource :: IfLetGuard => emit_diag ! ( IrrefutableLetPatternsIfLetGuard ) ,
912923 LetSource :: LetElse => emit_diag ! ( IrrefutableLetPatternsLetElse ) ,
913924 LetSource :: WhileLet => emit_diag ! ( IrrefutableLetPatternsWhileLet ) ,
0 commit comments