@@ -295,11 +295,21 @@ fn scan_block_for_eq(cx: &LateContext<'tcx>, blocks: &[&Block<'tcx>]) -> (usize,
295295 let l_stmts = win[ 0 ] . stmts ;
296296 let r_stmts = win[ 1 ] . stmts ;
297297
298+ // `SpanlessEq` now keeps track of the locals and is therefore context sensitive clippy#6752.
299+ // The comparison therefor needs to be done in a way that builds the correct context.
298300 let mut evaluator = SpanlessEq :: new ( cx) ;
301+ let mut evaluator = evaluator. inter_expr ( ) ;
302+
299303 let current_start_eq = count_eq ( & mut l_stmts. iter ( ) , & mut r_stmts. iter ( ) , |l, r| evaluator. eq_stmt ( l, r) ) ;
300- let current_end_eq = count_eq ( & mut l_stmts. iter ( ) . rev ( ) , & mut r_stmts. iter ( ) . rev ( ) , |l, r| {
301- evaluator. eq_stmt ( l, r)
302- } ) ;
304+
305+ let current_end_eq = {
306+ // We skip the middle statements which can't be equal
307+ let end_comparison_count = l_stmts. len ( ) . min ( r_stmts. len ( ) ) - current_start_eq;
308+ let it1 = l_stmts. iter ( ) . skip ( l_stmts. len ( ) - end_comparison_count) ;
309+ let it2 = r_stmts. iter ( ) . skip ( r_stmts. len ( ) - end_comparison_count) ;
310+ it1. zip ( it2)
311+ . fold ( 0 , |acc, ( l, r) | if evaluator. eq_stmt ( l, r) { acc + 1 } else { 0 } )
312+ } ;
303313 let block_expr_eq = both ( & win[ 0 ] . expr , & win[ 1 ] . expr , |l, r| evaluator. eq_expr ( l, r) ) ;
304314
305315 // IF_SAME_THEN_ELSE
@@ -458,8 +468,8 @@ fn emit_shared_code_in_if_blocks_lint(
458468 // Emit lint
459469 if suggestions. len ( ) == 1 {
460470 let ( place_str, span, sugg) = suggestions. pop ( ) . unwrap ( ) ;
461- let msg = format ! ( "All if blocks contain the same code at the {}" , place_str) ;
462- let help = format ! ( "Consider moving the {} statements out like this" , place_str) ;
471+ let msg = format ! ( "all if blocks contain the same code at the {}" , place_str) ;
472+ let help = format ! ( "consider moving the {} statements out like this" , place_str) ;
463473 span_lint_and_then ( cx, SHARED_CODE_IN_IF_BLOCKS , span, msg. as_str ( ) , |diag| {
464474 diag. span_suggestion ( span, help. as_str ( ) , sugg, Applicability :: Unspecified ) ;
465475
@@ -472,20 +482,20 @@ fn emit_shared_code_in_if_blocks_lint(
472482 cx,
473483 SHARED_CODE_IN_IF_BLOCKS ,
474484 start_span,
475- "All if blocks contain the same code at the start and the end. Here at the start: " ,
485+ "all if blocks contain the same code at the start and the end. Here at the start" ,
476486 move |diag| {
477- diag. span_note ( end_span, "And here at the end: " ) ;
487+ diag. span_note ( end_span, "and here at the end" ) ;
478488
479489 diag. span_suggestion (
480490 start_span,
481- "Consider moving the start statements out like this: " ,
491+ "consider moving the start statements out like this" ,
482492 start_sugg,
483493 Applicability :: Unspecified ,
484494 ) ;
485495
486496 diag. span_suggestion (
487497 end_span,
488- "And consider moving the end statements out like this: " ,
498+ "and consider moving the end statements out like this" ,
489499 end_sugg,
490500 Applicability :: Unspecified ,
491501 ) ;
0 commit comments