@@ -229,15 +229,15 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
229
229
} => {
230
230
err. span_label ( span, format ! ( "cannot {ACT}" , ACT = act) ) ;
231
231
232
- if let Some ( ( span, message ) ) = annotate_struct_field (
232
+ if let Some ( span) = get_mut_span_in_struct_field (
233
233
self . infcx . tcx ,
234
234
Place :: ty_from ( local, proj_base, self . body , self . infcx . tcx ) . ty ,
235
235
field,
236
236
) {
237
- err. span_suggestion (
237
+ err. span_suggestion_verbose (
238
238
span,
239
239
"consider changing this to be mutable" ,
240
- message ,
240
+ "mut " . into ( ) ,
241
241
Applicability :: MaybeIncorrect ,
242
242
) ;
243
243
}
@@ -1059,18 +1059,18 @@ fn is_closure_or_generator(ty: Ty<'_>) -> bool {
1059
1059
ty. is_closure ( ) || ty. is_generator ( )
1060
1060
}
1061
1061
1062
- /// Adds a suggestion to a struct definition given a field access to a local .
1063
- /// This function expects the local to be a reference to a struct in order to produce a suggestion .
1062
+ /// Given a field that needs to be mutuable, returns a span where the mut could go .
1063
+ /// This function expects the local to be a reference to a struct in order to produce a span .
1064
1064
///
1065
1065
/// ```text
1066
1066
/// LL | s: &'a String
1067
- /// | ---------- use `&'a mut String` here to make mutable
1067
+ /// | ^ returns a span pointing here
1068
1068
/// ```
1069
- fn annotate_struct_field < ' tcx > (
1069
+ fn get_mut_span_in_struct_field < ' tcx > (
1070
1070
tcx : TyCtxt < ' tcx > ,
1071
1071
ty : Ty < ' tcx > ,
1072
1072
field : & mir:: Field ,
1073
- ) -> Option < ( Span , String ) > {
1073
+ ) -> Option < Span > {
1074
1074
// Expect our local to be a reference to a struct of some kind.
1075
1075
if let ty:: Ref ( _, ty, _) = ty. kind ( ) {
1076
1076
if let ty:: Adt ( def, _) = ty. kind ( ) {
@@ -1081,25 +1081,12 @@ fn annotate_struct_field<'tcx>(
1081
1081
// Now we're dealing with the actual struct that we're going to suggest a change to,
1082
1082
// we can expect a field that is an immutable reference to a type.
1083
1083
if let hir:: Node :: Field ( field) = node {
1084
- if let hir:: TyKind :: Rptr (
1085
- lifetime,
1086
- hir:: MutTy { mutbl : hir:: Mutability :: Not , ref ty } ,
1087
- ) = field. ty . kind
1084
+ if let hir:: TyKind :: Rptr ( lifetime, hir:: MutTy { mutbl : hir:: Mutability :: Not , .. } ) =
1085
+ field. ty . kind
1088
1086
{
1089
- // Get the snippets in two parts - the named lifetime (if there is one) and
1090
- // type being referenced, that way we can reconstruct the snippet without loss
1091
- // of detail.
1092
- let type_snippet = tcx. sess . source_map ( ) . span_to_snippet ( ty. span ) . ok ( ) ?;
1093
- let lifetime_snippet = if !lifetime. is_elided ( ) {
1094
- format ! ( "{} " , tcx. sess. source_map( ) . span_to_snippet( lifetime. span) . ok( ) ?)
1095
- } else {
1096
- String :: new ( )
1097
- } ;
1098
-
1099
- return Some ( (
1100
- field. ty . span ,
1101
- format ! ( "&{}mut {}" , lifetime_snippet, & * type_snippet, ) ,
1102
- ) ) ;
1087
+ return Some (
1088
+ lifetime. span . with_hi ( lifetime. span . hi ( ) + BytePos ( 1 ) ) . shrink_to_hi ( ) ,
1089
+ ) ;
1103
1090
}
1104
1091
}
1105
1092
}
0 commit comments