@@ -79,6 +79,8 @@ enum LetSource {
79
79
IfLetGuard ,
80
80
LetElse ,
81
81
WhileLet ,
82
+ Else ,
83
+ ElseIfLet ,
82
84
}
83
85
84
86
struct MatchVisitor < ' p , ' tcx > {
@@ -129,15 +131,20 @@ impl<'p, 'tcx> Visitor<'p, 'tcx> for MatchVisitor<'p, 'tcx> {
129
131
// Give a specific `let_source` for the condition.
130
132
let let_source = match ex. span . desugaring_kind ( ) {
131
133
Some ( DesugaringKind :: WhileLoop ) => LetSource :: WhileLet ,
132
- _ => LetSource :: IfLet ,
134
+ _ => match self . let_source {
135
+ LetSource :: Else => LetSource :: ElseIfLet ,
136
+ _ => LetSource :: IfLet ,
137
+ } ,
133
138
} ;
134
139
self . with_let_source ( let_source, |this| this. visit_expr ( & self . thir [ cond] ) ) ;
135
140
self . with_let_source ( LetSource :: None , |this| {
136
141
this. visit_expr ( & this. thir [ then] ) ;
137
- if let Some ( else_) = else_opt {
138
- this. visit_expr ( & this. thir [ else_] ) ;
139
- }
140
142
} ) ;
143
+ if let Some ( else_) = else_opt {
144
+ self . with_let_source ( LetSource :: Else , |this| {
145
+ this. visit_expr ( & this. thir [ else_] )
146
+ } ) ;
147
+ }
141
148
return ;
142
149
}
143
150
ExprKind :: Match { scrutinee, scrutinee_hir_id : _, box ref arms, match_source } => {
@@ -569,9 +576,13 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
569
576
// and we shouldn't lint.
570
577
// For let guards inside a match, prefixes might use bindings of the match pattern,
571
578
// so can't always be moved out.
579
+ // For `else if let`, an extra indentation level would be required to move the bindings.
572
580
// FIXME: Add checking whether the bindings are actually used in the prefix,
573
581
// 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
+ ) {
575
586
// Emit the lint
576
587
let prefix = & chain_refutabilities[ ..until] ;
577
588
let span_start = prefix[ 0 ] . unwrap ( ) . 0 ;
@@ -902,8 +913,8 @@ fn report_irrefutable_let_patterns(
902
913
}
903
914
904
915
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 ) ,
907
918
LetSource :: IfLetGuard => emit_diag ! ( IrrefutableLetPatternsIfLetGuard ) ,
908
919
LetSource :: LetElse => emit_diag ! ( IrrefutableLetPatternsLetElse ) ,
909
920
LetSource :: WhileLet => emit_diag ! ( IrrefutableLetPatternsWhileLet ) ,
0 commit comments