@@ -160,7 +160,7 @@ impl<'p, 'tcx> Visitor<'p, 'tcx> for MatchVisitor<'p, 'tcx> {
160160 self . check_match ( scrutinee, arms, MatchSource :: Normal , span) ;
161161 }
162162 ExprKind :: Let { box ref pat, expr } => {
163- self . check_let ( pat, Some ( expr) , ex. span ) ;
163+ self . check_let ( pat, Some ( expr) , ex. span , None ) ;
164164 }
165165 ExprKind :: LogicalOp { op : LogicalOp :: And , .. }
166166 if !matches ! ( self . let_source, LetSource :: None ) =>
@@ -169,7 +169,7 @@ impl<'p, 'tcx> Visitor<'p, 'tcx> for MatchVisitor<'p, 'tcx> {
169169 let Ok ( ( ) ) = self . visit_land ( ex, & mut chain_refutabilities) else { return } ;
170170 // Lint only single irrefutable let binding.
171171 if let [ Some ( ( _, Irrefutable ) ) ] = chain_refutabilities[ ..] {
172- self . lint_single_let ( ex. span ) ;
172+ self . lint_single_let ( ex. span , None ) ;
173173 }
174174 return ;
175175 }
@@ -184,8 +184,9 @@ impl<'p, 'tcx> Visitor<'p, 'tcx> for MatchVisitor<'p, 'tcx> {
184184 self . with_hir_source ( hir_id, |this| {
185185 let let_source =
186186 if else_block. is_some ( ) { LetSource :: LetElse } else { LetSource :: PlainLet } ;
187+ let else_span = else_block. map ( |bid| this. thir . blocks [ bid] . span ) ;
187188 this. with_let_source ( let_source, |this| {
188- this. check_let ( pattern, initializer, span)
189+ this. check_let ( pattern, initializer, span, else_span )
189190 } ) ;
190191 visit:: walk_stmt ( this, stmt) ;
191192 } ) ;
@@ -426,13 +427,19 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
426427 }
427428
428429 #[ instrument( level = "trace" , skip( self ) ) ]
429- fn check_let ( & mut self , pat : & ' p Pat < ' tcx > , scrutinee : Option < ExprId > , span : Span ) {
430+ fn check_let (
431+ & mut self ,
432+ pat : & ' p Pat < ' tcx > ,
433+ scrutinee : Option < ExprId > ,
434+ span : Span ,
435+ else_span : Option < Span > ,
436+ ) {
430437 assert ! ( self . let_source != LetSource :: None ) ;
431438 let scrut = scrutinee. map ( |id| & self . thir [ id] ) ;
432439 if let LetSource :: PlainLet = self . let_source {
433440 self . check_binding_is_irrefutable ( pat, "local binding" , scrut, Some ( span) ) ;
434441 } else if let Ok ( Irrefutable ) = self . is_let_irrefutable ( pat, scrut) {
435- self . lint_single_let ( span) ;
442+ self . lint_single_let ( span, else_span ) ;
436443 }
437444 }
438445
@@ -540,8 +547,15 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
540547 }
541548
542549 #[ instrument( level = "trace" , skip( self ) ) ]
543- fn lint_single_let ( & mut self , let_span : Span ) {
544- report_irrefutable_let_patterns ( self . tcx , self . hir_source , self . let_source , 1 , let_span) ;
550+ fn lint_single_let ( & mut self , let_span : Span , else_span : Option < Span > ) {
551+ report_irrefutable_let_patterns (
552+ self . tcx ,
553+ self . hir_source ,
554+ self . let_source ,
555+ 1 ,
556+ let_span,
557+ else_span,
558+ ) ;
545559 }
546560
547561 fn analyze_binding (
@@ -836,6 +850,7 @@ fn report_irrefutable_let_patterns(
836850 source : LetSource ,
837851 count : usize ,
838852 span : Span ,
853+ else_span : Option < Span > ,
839854) {
840855 macro_rules! emit_diag {
841856 ( $lint: tt) => { {
@@ -847,7 +862,14 @@ fn report_irrefutable_let_patterns(
847862 LetSource :: None | LetSource :: PlainLet | LetSource :: Else => bug ! ( ) ,
848863 LetSource :: IfLet | LetSource :: ElseIfLet => emit_diag ! ( IrrefutableLetPatternsIfLet ) ,
849864 LetSource :: IfLetGuard => emit_diag ! ( IrrefutableLetPatternsIfLetGuard ) ,
850- LetSource :: LetElse => emit_diag ! ( IrrefutableLetPatternsLetElse ) ,
865+ LetSource :: LetElse => {
866+ tcx. emit_node_span_lint (
867+ IRREFUTABLE_LET_PATTERNS ,
868+ id,
869+ span,
870+ IrrefutableLetPatternsLetElse { count, else_span } ,
871+ ) ;
872+ }
851873 LetSource :: WhileLet => emit_diag ! ( IrrefutableLetPatternsWhileLet ) ,
852874 }
853875}
0 commit comments