@@ -229,15 +229,15 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
229229 } => {
230230 err. span_label ( span, format ! ( "cannot {ACT}" , ACT = act) ) ;
231231
232- if let Some ( ( span, message ) ) = annotate_struct_field (
232+ if let Some ( span) = get_mut_span_in_struct_field (
233233 self . infcx . tcx ,
234234 Place :: ty_from ( local, proj_base, self . body , self . infcx . tcx ) . ty ,
235235 field,
236236 ) {
237- err. span_suggestion (
237+ err. span_suggestion_verbose (
238238 span,
239239 "consider changing this to be mutable" ,
240- message ,
240+ " mut " . into ( ) ,
241241 Applicability :: MaybeIncorrect ,
242242 ) ;
243243 }
@@ -1059,18 +1059,18 @@ fn is_closure_or_generator(ty: Ty<'_>) -> bool {
10591059 ty. is_closure ( ) || ty. is_generator ( )
10601060}
10611061
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 mutable, 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 .
10641064///
10651065/// ```text
1066- /// LL | s: &'a String
1067- /// | ---------- use `&'a mut String` here to make mutable
1066+ /// LL | s: &'a String
1067+ /// | ^^^ returns a span taking up the space here
10681068/// ```
1069- fn annotate_struct_field < ' tcx > (
1069+ fn get_mut_span_in_struct_field < ' tcx > (
10701070 tcx : TyCtxt < ' tcx > ,
10711071 ty : Ty < ' tcx > ,
10721072 field : & mir:: Field ,
1073- ) -> Option < ( Span , String ) > {
1073+ ) -> Option < Span > {
10741074 // Expect our local to be a reference to a struct of some kind.
10751075 if let ty:: Ref ( _, ty, _) = ty. kind ( ) {
10761076 if let ty:: Adt ( def, _) = ty. kind ( ) {
@@ -1081,25 +1081,10 @@ fn annotate_struct_field<'tcx>(
10811081 // Now we're dealing with the actual struct that we're going to suggest a change to,
10821082 // we can expect a field that is an immutable reference to a type.
10831083 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 , ty } ) =
1085+ field. ty . kind
10881086 {
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 ( lifetime. span . between ( ty. span ) ) ;
11031088 }
11041089 }
11051090 }
0 commit comments