@@ -167,9 +167,9 @@ impl<'p, 'tcx> Visitor<'p, 'tcx> for MatchVisitor<'p, 'tcx> {
167167 {
168168 let mut chain_refutabilities = Vec :: new ( ) ;
169169 let Ok ( ( ) ) = self . visit_land ( ex, & mut chain_refutabilities) else { return } ;
170- // If at least one of the operands is a ` let ... = ...` .
171- if chain_refutabilities . iter ( ) . any ( |x| x . is_some ( ) ) {
172- self . check_let_chain ( chain_refutabilities , ex. span ) ;
170+ // Lint only single irrefutable let binding .
171+ if let [ Some ( ( _ , Irrefutable ) ) ] = chain_refutabilities [ .. ] {
172+ self . lint_single_let ( ex. span ) ;
173173 }
174174 return ;
175175 }
@@ -430,18 +430,9 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
430430 assert ! ( self . let_source != LetSource :: None ) ;
431431 let scrut = scrutinee. map ( |id| & self . thir [ id] ) ;
432432 if let LetSource :: PlainLet = self . let_source {
433- self . check_binding_is_irrefutable ( pat, "local binding" , scrut, Some ( span) )
434- } else {
435- let Ok ( refutability) = self . is_let_irrefutable ( pat, scrut) else { return } ;
436- if matches ! ( refutability, Irrefutable ) {
437- report_irrefutable_let_patterns (
438- self . tcx ,
439- self . hir_source ,
440- self . let_source ,
441- 1 ,
442- span,
443- ) ;
444- }
433+ self . check_binding_is_irrefutable ( pat, "local binding" , scrut, Some ( span) ) ;
434+ } else if let Ok ( Irrefutable ) = self . is_let_irrefutable ( pat, scrut) {
435+ self . lint_single_let ( span) ;
445436 }
446437 }
447438
@@ -549,74 +540,8 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
549540 }
550541
551542 #[ instrument( level = "trace" , skip( self ) ) ]
552- fn check_let_chain (
553- & mut self ,
554- chain_refutabilities : Vec < Option < ( Span , RefutableFlag ) > > ,
555- whole_chain_span : Span ,
556- ) {
557- assert ! ( self . let_source != LetSource :: None ) ;
558-
559- if chain_refutabilities. iter ( ) . all ( |r| matches ! ( * r, Some ( ( _, Irrefutable ) ) ) ) {
560- // The entire chain is made up of irrefutable `let` statements
561- report_irrefutable_let_patterns (
562- self . tcx ,
563- self . hir_source ,
564- self . let_source ,
565- chain_refutabilities. len ( ) ,
566- whole_chain_span,
567- ) ;
568- return ;
569- }
570-
571- if let Some ( until) =
572- chain_refutabilities. iter ( ) . position ( |r| !matches ! ( * r, Some ( ( _, Irrefutable ) ) ) )
573- && until > 0
574- {
575- // The chain has a non-zero prefix of irrefutable `let` statements.
576-
577- // Check if the let source is while, for there is no alternative place to put a prefix,
578- // and we shouldn't lint.
579- // For let guards inside a match, prefixes might use bindings of the match pattern,
580- // so can't always be moved out.
581- // For `else if let`, an extra indentation level would be required to move the bindings.
582- // FIXME: Add checking whether the bindings are actually used in the prefix,
583- // and lint if they are not.
584- if !matches ! (
585- self . let_source,
586- LetSource :: WhileLet | LetSource :: IfLetGuard | LetSource :: ElseIfLet
587- ) {
588- // Emit the lint
589- let prefix = & chain_refutabilities[ ..until] ;
590- let span_start = prefix[ 0 ] . unwrap ( ) . 0 ;
591- let span_end = prefix. last ( ) . unwrap ( ) . unwrap ( ) . 0 ;
592- let span = span_start. to ( span_end) ;
593- let count = prefix. len ( ) ;
594- self . tcx . emit_node_span_lint (
595- IRREFUTABLE_LET_PATTERNS ,
596- self . hir_source ,
597- span,
598- LeadingIrrefutableLetPatterns { count } ,
599- ) ;
600- }
601- }
602-
603- if let Some ( from) =
604- chain_refutabilities. iter ( ) . rposition ( |r| !matches ! ( * r, Some ( ( _, Irrefutable ) ) ) )
605- && from != ( chain_refutabilities. len ( ) - 1 )
606- {
607- // The chain has a non-empty suffix of irrefutable `let` statements
608- let suffix = & chain_refutabilities[ from + 1 ..] ;
609- let span_start = suffix[ 0 ] . unwrap ( ) . 0 ;
610- let span_end = suffix. last ( ) . unwrap ( ) . unwrap ( ) . 0 ;
611- let span = span_start. to ( span_end) ;
612- let count = suffix. len ( ) ;
613- self . tcx . emit_node_span_lint (
614- IRREFUTABLE_LET_PATTERNS ,
615- self . hir_source ,
616- span,
617- TrailingIrrefutableLetPatterns { count } ,
618- ) ;
619- }
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) ;
620545 }
621546
622547 fn analyze_binding (
0 commit comments