@@ -1334,52 +1334,55 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13341334 ) ) ;
13351335 }
13361336
1337- if let Ok ( src) = sm. span_to_snippet ( sugg_sp) {
1338- let needs_parens = match expr. kind {
1339- // parenthesize if needed (Issue #46756)
1340- hir:: ExprKind :: Cast ( _, _) | hir:: ExprKind :: Binary ( _, _, _) => true ,
1341- // parenthesize borrows of range literals (Issue #54505)
1342- _ if is_range_literal ( expr) => true ,
1343- _ => false ,
1344- } ;
1345-
1346- if let Some ( sugg) = self . can_use_as_ref ( expr) {
1347- return Some ( (
1348- sugg. 0 ,
1349- sugg. 1 . to_string ( ) ,
1350- sugg. 2 ,
1351- Applicability :: MachineApplicable ,
1352- false ,
1353- false ,
1354- ) ) ;
1355- }
1356-
1357- let prefix = match self . maybe_get_struct_pattern_shorthand_field ( expr) {
1358- Some ( ident) => format ! ( "{ident}: " ) ,
1359- None => String :: new ( ) ,
1360- } ;
1361-
1362- if let Some ( hir:: Node :: Expr ( hir:: Expr {
1363- kind : hir:: ExprKind :: Assign ( ..) ,
1364- ..
1365- } ) ) = self . tcx . hir ( ) . find_parent ( expr. hir_id )
1366- {
1367- if mutability. is_mut ( ) {
1368- // Suppressing this diagnostic, we'll properly print it in `check_expr_assign`
1369- return None ;
1370- }
1371- }
1337+ let needs_parens = match expr. kind {
1338+ // parenthesize if needed (Issue #46756)
1339+ hir:: ExprKind :: Cast ( _, _) | hir:: ExprKind :: Binary ( _, _, _) => true ,
1340+ // parenthesize borrows of range literals (Issue #54505)
1341+ _ if is_range_literal ( expr) => true ,
1342+ _ => false ,
1343+ } ;
13721344
1373- let sugg_expr = if needs_parens { format ! ( "({src})" ) } else { src } ;
1345+ if let Some ( sugg ) = self . can_use_as_ref ( expr ) {
13741346 return Some ( (
1375- sp ,
1376- format ! ( "consider {}borrowing here" , mutability . mutably_str ( ) ) ,
1377- format ! ( "{prefix}{}{sugg_expr}" , mutability . ref_prefix_str ( ) ) ,
1347+ sugg . 0 ,
1348+ sugg . 1 . to_string ( ) ,
1349+ sugg . 2 ,
13781350 Applicability :: MachineApplicable ,
13791351 false ,
13801352 false ,
13811353 ) ) ;
13821354 }
1355+
1356+ let prefix = match self . maybe_get_struct_pattern_shorthand_field ( expr) {
1357+ Some ( ident) => format ! ( "{ident}: " ) ,
1358+ None => String :: new ( ) ,
1359+ } ;
1360+
1361+ if let Some ( hir:: Node :: Expr ( hir:: Expr {
1362+ kind : hir:: ExprKind :: Assign ( ..) ,
1363+ ..
1364+ } ) ) = self . tcx . hir ( ) . find_parent ( expr. hir_id )
1365+ {
1366+ if mutability. is_mut ( ) {
1367+ // Suppressing this diagnostic, we'll properly print it in `check_expr_assign`
1368+ return None ;
1369+ }
1370+ }
1371+
1372+ let ( sp, sugg_expr, verbose) = if needs_parens {
1373+ let src = sm. span_to_snippet ( sugg_sp) . ok ( ) ?;
1374+ ( sp, format ! ( "({src})" ) , false )
1375+ } else {
1376+ ( sp. shrink_to_lo ( ) , "" . to_string ( ) , true )
1377+ } ;
1378+ return Some ( (
1379+ sp,
1380+ format ! ( "consider {}borrowing here" , mutability. mutably_str( ) ) ,
1381+ format ! ( "{prefix}{}{sugg_expr}" , mutability. ref_prefix_str( ) ) ,
1382+ Applicability :: MachineApplicable ,
1383+ verbose,
1384+ false ,
1385+ ) ) ;
13831386 }
13841387 }
13851388 (
0 commit comments