@@ -147,7 +147,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
147
147
if let Some ( desc) = access_place_desc {
148
148
item_msg = format ! ( "`{}`" , desc) ;
149
149
reason = match error_access {
150
- AccessKind :: Mutate => format ! ( " which is behind {}" , pointer_type) ,
150
+ AccessKind :: Mutate => format ! ( ", which is behind {}" , pointer_type) ,
151
151
AccessKind :: MutableBorrow => {
152
152
format ! ( ", as it is behind {}" , pointer_type)
153
153
}
@@ -897,16 +897,32 @@ fn suggest_ampmut<'tcx>(
897
897
) -> ( Span , String ) {
898
898
if let Some ( assignment_rhs_span) = opt_assignment_rhs_span {
899
899
if let Ok ( src) = tcx. sess . source_map ( ) . span_to_snippet ( assignment_rhs_span) {
900
+ let is_mutbl = |ty : & str | -> bool {
901
+ if ty. starts_with ( "mut" ) {
902
+ let rest = & ty[ 3 ..] ;
903
+ match rest. chars ( ) . next ( ) {
904
+ // e.g. `&mut x`
905
+ Some ( c) if c. is_whitespace ( ) => true ,
906
+ // e.g. `&mut(x)`
907
+ Some ( '(' ) => true ,
908
+ // e.g. `&mutablevar`
909
+ _ => false ,
910
+ }
911
+ } else {
912
+ false
913
+ }
914
+ } ;
900
915
if let ( true , Some ( ws_pos) ) =
901
916
( src. starts_with ( "&'" ) , src. find ( |c : char | -> bool { c. is_whitespace ( ) } ) )
902
917
{
903
918
let lt_name = & src[ 1 ..ws_pos] ;
904
- let ty = & src[ ws_pos..] ;
905
- if !ty . trim_start ( ) . starts_with ( "mut" ) {
919
+ let ty = src[ ws_pos..] . trim_start ( ) ;
920
+ if !is_mutbl ( ty ) {
906
921
return ( assignment_rhs_span, format ! ( "&{} mut {}" , lt_name, ty) ) ;
907
922
}
908
923
} else if let Some ( stripped) = src. strip_prefix ( '&' ) {
909
- if !stripped. trim_start ( ) . starts_with ( "mut" ) {
924
+ let stripped = stripped. trim_start ( ) ;
925
+ if !is_mutbl ( stripped) {
910
926
return ( assignment_rhs_span, format ! ( "&mut {}" , stripped) ) ;
911
927
}
912
928
}
0 commit comments