@@ -442,12 +442,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
442
442
expected,
443
443
)
444
444
} ) ;
445
+
446
+ let prefix_wrap = |sugg : & str | {
447
+ if let Some ( name) = self . tcx . hir ( ) . maybe_get_struct_pattern_shorthand_field ( expr) {
448
+ format ! ( ": {}{}" , name, sugg)
449
+ } else {
450
+ sugg. to_string ( )
451
+ }
452
+ } ;
453
+
445
454
// FIXME: This could/should be extended to suggest `as_mut` and `as_deref_mut`,
446
455
// but those checks need to be a bit more delicate and the benefit is diminishing.
447
456
if self . can_eq ( self . param_env , found_ty_inner, peeled) && error_tys_equate_as_ref {
457
+ let sugg = prefix_wrap ( ".as_ref()" ) ;
448
458
err. subdiagnostic ( errors:: SuggestConvertViaMethod {
449
459
span : expr. span . shrink_to_hi ( ) ,
450
- sugg : ".as_ref()" ,
460
+ sugg,
451
461
expected,
452
462
found,
453
463
borrow_removal_span,
@@ -458,9 +468,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
458
468
&& self . can_eq ( self . param_env , deref_ty, peeled)
459
469
&& error_tys_equate_as_ref
460
470
{
471
+ let sugg = prefix_wrap ( ".as_deref()" ) ;
461
472
err. subdiagnostic ( errors:: SuggestConvertViaMethod {
462
473
span : expr. span . shrink_to_hi ( ) ,
463
- sugg : ".as_deref()" ,
474
+ sugg,
464
475
expected,
465
476
found,
466
477
borrow_removal_span,
@@ -474,10 +485,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
474
485
self . can_eq ( self . param_env , found, expected)
475
486
} )
476
487
{
488
+ let sugg = prefix_wrap ( ".map(|x| x.as_str())" ) ;
477
489
err. span_suggestion_verbose (
478
490
expr. span . shrink_to_hi ( ) ,
479
491
fluent:: hir_typeck_convert_to_str,
480
- ".map(|x| x.as_str())" ,
492
+ sugg ,
481
493
Applicability :: MachineApplicable ,
482
494
) ;
483
495
return true ;
@@ -628,12 +640,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
628
640
err. help ( "use `Box::pin`" ) ;
629
641
}
630
642
_ => {
643
+ let prefix = if let Some ( name) =
644
+ self . tcx . hir ( ) . maybe_get_struct_pattern_shorthand_field ( expr)
645
+ {
646
+ format ! ( "{}: " , name)
647
+ } else {
648
+ String :: new ( )
649
+ } ;
650
+ let suggestion = vec ! [
651
+ ( expr. span. shrink_to_lo( ) , format!( "{prefix}Box::pin(" ) ) ,
652
+ ( expr. span. shrink_to_hi( ) , ")" . to_string( ) ) ,
653
+ ] ;
631
654
err. multipart_suggestion (
632
655
"you need to pin and box this expression" ,
633
- vec ! [
634
- ( expr. span. shrink_to_lo( ) , "Box::pin(" . to_string( ) ) ,
635
- ( expr. span. shrink_to_hi( ) , ")" . to_string( ) ) ,
636
- ] ,
656
+ suggestion,
637
657
Applicability :: MaybeIncorrect ,
638
658
) ;
639
659
}
@@ -1214,14 +1234,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1214
1234
span = parent_callsite;
1215
1235
}
1216
1236
1217
- let sugg = if expr. precedence ( ) . order ( ) >= PREC_POSTFIX {
1237
+ let mut sugg = if expr. precedence ( ) . order ( ) >= PREC_POSTFIX {
1218
1238
vec ! [ ( span. shrink_to_hi( ) , ".into()" . to_owned( ) ) ]
1219
1239
} else {
1220
1240
vec ! [
1221
1241
( span. shrink_to_lo( ) , "(" . to_owned( ) ) ,
1222
1242
( span. shrink_to_hi( ) , ").into()" . to_owned( ) ) ,
1223
1243
]
1224
1244
} ;
1245
+ if let Some ( name) = self . tcx . hir ( ) . maybe_get_struct_pattern_shorthand_field ( expr) {
1246
+ sugg. insert ( 0 , ( expr. span . shrink_to_lo ( ) , format ! ( "{}: " , name) ) ) ;
1247
+ }
1225
1248
diag. multipart_suggestion (
1226
1249
format ! ( "call `Into::into` on this expression to convert `{expr_ty}` into `{expected_ty}`" ) ,
1227
1250
sugg,
@@ -1811,6 +1834,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1811
1834
".expect(\" REASON\" )" ,
1812
1835
)
1813
1836
} ;
1837
+
1838
+ let sugg = match self . tcx . hir ( ) . maybe_get_struct_pattern_shorthand_field ( expr) {
1839
+ Some ( ident) => format ! ( ": {ident}{sugg}" ) ,
1840
+ None => sugg. to_string ( ) ,
1841
+ } ;
1842
+
1814
1843
err. span_suggestion_verbose (
1815
1844
expr. span . shrink_to_hi ( ) ,
1816
1845
msg,
0 commit comments