@@ -147,7 +147,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
147147 if let Some ( desc) = access_place_desc {
148148 item_msg = format ! ( "`{}`" , desc) ;
149149 reason = match error_access {
150- AccessKind :: Mutate => format ! ( " which is behind {}" , pointer_type) ,
150+ AccessKind :: Mutate => format ! ( ", which is behind {}" , pointer_type) ,
151151 AccessKind :: MutableBorrow => {
152152 format ! ( ", as it is behind {}" , pointer_type)
153153 }
@@ -897,16 +897,32 @@ fn suggest_ampmut<'tcx>(
897897) -> ( Span , String ) {
898898 if let Some ( assignment_rhs_span) = opt_assignment_rhs_span {
899899 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+ } ;
900915 if let ( true , Some ( ws_pos) ) =
901916 ( src. starts_with ( "&'" ) , src. find ( |c : char | -> bool { c. is_whitespace ( ) } ) )
902917 {
903918 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 ) {
906921 return ( assignment_rhs_span, format ! ( "&{} mut {}" , lt_name, ty) ) ;
907922 }
908923 } 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) {
910926 return ( assignment_rhs_span, format ! ( "&mut {}" , stripped) ) ;
911927 }
912928 }
0 commit comments