@@ -128,7 +128,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
128128 && pat. span . can_be_used_for_suggestions ( )
129129 && let Ok ( pat) = tcx. sess . source_map ( ) . span_to_snippet ( pat. span )
130130 {
131- suggest_rewrite_if_let ( expr, & pat, init, conseq, alt, err) ;
131+ suggest_rewrite_if_let ( tcx , expr, & pat, init, conseq, alt, err) ;
132132 } else if path_span. map_or ( true , |path_span| path_span == var_or_use_span) {
133133 // We can use `var_or_use_span` if either `path_span` is not present, or both spans are the same
134134 if borrow_span. map_or ( true , |sp| !sp. overlaps ( var_or_use_span) ) {
@@ -292,7 +292,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
292292 && pat. span . can_be_used_for_suggestions ( )
293293 && let Ok ( pat) = tcx. sess . source_map ( ) . span_to_snippet ( pat. span )
294294 {
295- suggest_rewrite_if_let ( expr, & pat, init, conseq, alt, err) ;
295+ suggest_rewrite_if_let ( tcx , expr, & pat, init, conseq, alt, err) ;
296296 }
297297 }
298298 }
@@ -428,34 +428,49 @@ impl<'tcx> BorrowExplanation<'tcx> {
428428 }
429429}
430430
431- fn suggest_rewrite_if_let < ' tcx > (
432- expr : & hir:: Expr < ' tcx > ,
431+ fn suggest_rewrite_if_let (
432+ tcx : TyCtxt < ' _ > ,
433+ expr : & hir:: Expr < ' _ > ,
433434 pat : & str ,
434- init : & hir:: Expr < ' tcx > ,
435- conseq : & hir:: Expr < ' tcx > ,
436- alt : Option < & hir:: Expr < ' tcx > > ,
435+ init : & hir:: Expr < ' _ > ,
436+ conseq : & hir:: Expr < ' _ > ,
437+ alt : Option < & hir:: Expr < ' _ > > ,
437438 err : & mut Diag < ' _ > ,
438439) {
440+ let source_map = tcx. sess . source_map ( ) ;
439441 err. span_note (
440- conseq. span . shrink_to_hi ( ) ,
441- "lifetime for temporaries generated in `if let`s have been shorted in Edition 2024" ,
442+ source_map . end_point ( conseq. span ) ,
443+ "lifetimes for temporaries generated in `if let`s have been shortened in Edition 2024 so that they are dropped here instead " ,
442444 ) ;
443445 if expr. span . can_be_used_for_suggestions ( ) && conseq. span . can_be_used_for_suggestions ( ) {
446+ let needs_block = if let Some ( hir:: Node :: Expr ( expr) ) =
447+ alt. and_then ( |alt| tcx. hir ( ) . parent_iter ( alt. hir_id ) . next ( ) ) . map ( |( _, node) | node)
448+ {
449+ matches ! ( expr. kind, hir:: ExprKind :: If ( ..) )
450+ } else {
451+ false
452+ } ;
444453 let mut sugg = vec ! [
445- ( expr. span. shrink_to_lo( ) . between( init. span) , "match " . into( ) ) ,
454+ (
455+ expr. span. shrink_to_lo( ) . between( init. span) ,
456+ if needs_block { "{ match " . into( ) } else { "match " . into( ) } ,
457+ ) ,
446458 ( conseq. span. shrink_to_lo( ) , format!( " {{ {pat} => " ) ) ,
447459 ] ;
448460 let expr_end = expr. span . shrink_to_hi ( ) ;
461+ let mut expr_end_code;
449462 if let Some ( alt) = alt {
450- sugg. push ( ( conseq. span . between ( alt. span ) , format ! ( " _ => " ) ) ) ;
451- sugg . push ( ( expr_end , "}" . into ( ) ) ) ;
463+ sugg. push ( ( conseq. span . between ( alt. span ) , " _ => " . into ( ) ) ) ;
464+ expr_end_code = "}" . to_string ( ) ;
452465 } else {
453- sugg . push ( ( expr_end , " _ => {} }" . into ( ) ) ) ;
466+ expr_end_code = " _ => {} }" . into ( ) ;
454467 }
468+ expr_end_code. push ( '}' ) ;
469+ sugg. push ( ( expr_end, expr_end_code) ) ;
455470 err. multipart_suggestion (
456471 "consider rewriting the `if` into `match` which preserves the extended lifetime" ,
457472 sugg,
458- Applicability :: MachineApplicable ,
473+ Applicability :: MaybeIncorrect ,
459474 ) ;
460475 }
461476}
0 commit comments