@@ -45,7 +45,8 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
4545 match projection {
4646 ProjectionElem :: Deref => {
4747 if base_place. base == PlaceBase :: Local ( Local :: new ( 1 ) )
48- && !self . mir . upvar_decls . is_empty ( ) {
48+ && !self . mir . upvar_decls . is_empty ( )
49+ {
4950 item_msg = format ! ( "`{}`" , access_place_desc. unwrap( ) ) ;
5051 debug_assert ! ( self . mir. local_decls[ Local :: new( 1 ) ] . ty. is_region_ptr( ) ) ;
5152 debug_assert ! ( is_closure_or_generator(
@@ -59,8 +60,9 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
5960 }
6061 } else if {
6162 if let PlaceBase :: Local ( local) = base_place. base {
62- if let Some ( ClearCrossCrate :: Set ( BindingForm :: RefForGuard ) )
63- = self . mir . local_decls [ local] . is_user_variable {
63+ if let Some ( ClearCrossCrate :: Set ( BindingForm :: RefForGuard ) ) =
64+ self . mir . local_decls [ local] . is_user_variable
65+ {
6466 true
6567 } else {
6668 false
@@ -72,18 +74,21 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
7274 item_msg = format ! ( "`{}`" , access_place_desc. unwrap( ) ) ;
7375 reason = ", as it is immutable for the pattern guard" . to_string ( ) ;
7476 } else {
75- let pointer_type =
76- if base_place. ty ( self . mir , self . tcx ) . to_ty ( self . tcx ) . is_region_ptr ( ) {
77- "`&` reference"
78- } else {
79- "`*const` pointer"
80- } ;
77+ let pointer_type = if base_place
78+ . ty ( self . mir , self . tcx )
79+ . to_ty ( self . tcx )
80+ . is_region_ptr ( )
81+ {
82+ "`&` reference"
83+ } else {
84+ "`*const` pointer"
85+ } ;
8186 if let Some ( desc) = access_place_desc {
8287 item_msg = format ! ( "`{}`" , desc) ;
8388 reason = match error_access {
84- AccessKind :: Move |
85- AccessKind :: Mutate =>
86- format ! ( " which is behind a {}" , pointer_type ) ,
89+ AccessKind :: Move | AccessKind :: Mutate => {
90+ format ! ( " which is behind a {}" , pointer_type )
91+ }
8792 AccessKind :: MutableBorrow => {
8893 format ! ( ", as it is behind a {}" , pointer_type)
8994 }
@@ -93,7 +98,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
9398 reason = "" . to_string ( ) ;
9499 }
95100 }
96- } ,
101+ }
97102 ProjectionElem :: Field ( upvar_index, _) => {
98103 debug_assert ! ( is_closure_or_generator(
99104 base_place. ty( self . mir, self . tcx) . to_ty( self . tcx)
@@ -110,9 +115,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
110115 ProjectionElem :: Subslice { .. }
111116 | ProjectionElem :: Downcast ( ..)
112117 | ProjectionElem :: ConstantIndex { .. }
113- | ProjectionElem :: Index ( _) => {
114- bug ! ( "Unexpected immutable place." )
115- }
118+ | ProjectionElem :: Index ( _) => bug ! ( "Unexpected immutable place." ) ,
116119 }
117120 } else {
118121 match the_place_err. base {
@@ -129,10 +132,8 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
129132 }
130133 PlaceBase :: Static ( box Static { def_id, ty : _ } ) => {
131134 if let PlaceBase :: Static ( _) = access_place. base {
132- item_msg = format ! (
133- "immutable static item `{}`" ,
134- access_place_desc. unwrap( )
135- ) ;
135+ item_msg =
136+ format ! ( "immutable static item `{}`" , access_place_desc. unwrap( ) ) ;
136137 reason = "" . to_string ( ) ;
137138 } else {
138139 item_msg = format ! ( "`{}`" , access_place_desc. unwrap( ) ) ;
@@ -151,14 +152,16 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
151152
152153 let span = match error_access {
153154 AccessKind :: Move => {
154- err = self . tcx
155+ err = self
156+ . tcx
155157 . cannot_move_out_of ( span, & ( item_msg + & reason) , Origin :: Mir ) ;
156158 act = "move" ;
157159 acted_on = "moved" ;
158160 span
159161 }
160162 AccessKind :: Mutate => {
161- err = self . tcx
163+ err = self
164+ . tcx
162165 . cannot_assign ( span, & ( item_msg + & reason) , Origin :: Mir ) ;
163166 act = "assign" ;
164167 acted_on = "written" ;
@@ -182,7 +185,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
182185 "mutable borrow occurs due to use of `{}` in closure" ,
183186 // always Some() if the message is printed.
184187 self . describe_place( access_place) . unwrap_or( String :: new( ) ) ,
185- )
188+ ) ,
186189 ) ;
187190 borrow_span
188191 }
@@ -197,53 +200,52 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
197200 err. span_label ( span, format ! ( "cannot {ACT}" , ACT = act) ) ;
198201 err. note (
199202 "variables bound in patterns are \
200- immutable until the end of the pattern guard",
203+ immutable until the end of the pattern guard",
201204 ) ;
202- } ,
205+ }
203206 Some ( _) => {
204207 // We want to point out when a `&` can be readily replaced
205208 // with an `&mut`.
206209 //
207210 // FIXME: can this case be generalized to work for an
208211 // arbitrary base for the projection?
209212 let local_decl = & self . mir . local_decls [ local] ;
210- let suggestion =
211- match local_decl . is_user_variable . as_ref ( ) . unwrap ( ) {
213+ let suggestion = match local_decl . is_user_variable . as_ref ( ) . unwrap ( )
214+ {
212215 ClearCrossCrate :: Set ( mir:: BindingForm :: ImplicitSelf ) => {
213216 Some ( suggest_ampmut_self ( self . tcx , local_decl) )
214217 }
215218
216- ClearCrossCrate :: Set (
217- mir:: BindingForm :: Var (
218- mir :: VarBindingForm {
219- binding_mode : ty :: BindingMode :: BindByValue ( _ ) ,
220- opt_ty_info ,
221- ..
222- } ) ) => Some ( suggest_ampmut (
219+ ClearCrossCrate :: Set ( mir :: BindingForm :: Var (
220+ mir:: VarBindingForm {
221+ binding_mode : ty :: BindingMode :: BindByValue ( _ ) ,
222+ opt_ty_info ,
223+ ..
224+ } ,
225+ ) ) => Some ( suggest_ampmut (
223226 self . tcx ,
224227 self . mir ,
225228 local,
226229 local_decl,
227230 * opt_ty_info,
228231 ) ) ,
229232
230- ClearCrossCrate :: Set (
231- mir:: BindingForm :: Var (
232- mir:: VarBindingForm {
233- binding_mode : ty:: BindingMode :: BindByReference ( _) ,
234- ..
235- } )
236- ) => suggest_ref_mut ( self . tcx , local_decl. source_info . span ) ,
233+ ClearCrossCrate :: Set ( mir:: BindingForm :: Var (
234+ mir:: VarBindingForm {
235+ binding_mode : ty:: BindingMode :: BindByReference ( _) ,
236+ ..
237+ } ,
238+ ) ) => suggest_ref_mut ( self . tcx , local_decl. source_info . span ) ,
237239
238- ClearCrossCrate :: Set (
239- mir :: BindingForm :: RefForGuard
240- ) => unreachable ! ( ) ,
240+ ClearCrossCrate :: Set ( mir :: BindingForm :: RefForGuard ) => {
241+ unreachable ! ( )
242+ }
241243
242244 ClearCrossCrate :: Clear => bug ! ( "saw cleared local state" ) ,
243245 } ;
244246
245- let ( pointer_sigil, pointer_desc) =
246- if local_decl . ty . is_region_ptr ( ) {
247+ let ( pointer_sigil, pointer_desc) = if local_decl . ty . is_region_ptr ( )
248+ {
247249 ( "&" , "reference" )
248250 } else {
249251 ( "*const" , "pointer" )
@@ -265,7 +267,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
265267 span,
266268 format ! (
267269 "`{NAME}` is a `{SIGIL}` {DESC}, \
268- so the data it refers to cannot be {ACTED_ON}",
270+ so the data it refers to cannot be {ACTED_ON}",
269271 NAME = name,
270272 SIGIL = pointer_sigil,
271273 DESC = pointer_desc,
@@ -284,17 +286,17 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
284286 ) ;
285287 }
286288 }
287- _ => { } ,
289+ _ => { }
288290 }
289291 if local == Local :: new ( 1 ) && !self . mir . upvar_decls . is_empty ( ) {
290292 err. span_label ( span, format ! ( "cannot {ACT}" , ACT = act) ) ;
291293 err. span_help (
292294 self . mir . span ,
293- "consider changing this to accept closures that implement `FnMut`"
295+ "consider changing this to accept closures that implement `FnMut`" ,
294296 ) ;
295297 }
296298 }
297- } ,
299+ }
298300 ProjectionElem :: Field ( upvar_index, _) => {
299301 // Suggest adding mut for upvars
300302 debug_assert ! ( is_closure_or_generator(
@@ -314,16 +316,16 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
314316 upvar_ident,
315317 _,
316318 ) = pat. node
317- {
318- err. span_suggestion (
319- upvar_ident. span ,
320- "consider changing this to be mutable" ,
321- format ! ( "mut {}" , upvar_ident. name) ,
322- ) ;
323- }
319+ {
320+ err. span_suggestion (
321+ upvar_ident. span ,
322+ "consider changing this to be mutable" ,
323+ format ! ( "mut {}" , upvar_ident. name) ,
324+ ) ;
325+ }
324326 }
325327 }
326- _ => { } ,
328+ _ => { }
327329 }
328330 } else if let PlaceBase :: Local ( local) = the_place_err. base {
329331 if self . mir . local_decls [ local] . can_be_made_mutable ( ) {
@@ -367,10 +369,9 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
367369 is_closure_or_generator ( base_ty)
368370 }
369371 ProjectionElem :: Deref => {
370- if let (
371- ref base_place,
372- Some ( ProjectionElem :: Field ( upvar_index, _) ) ,
373- ) = base_place. split_projection ( self . tcx ) {
372+ if let ( ref base_place, Some ( ProjectionElem :: Field ( upvar_index, _) ) ) =
373+ base_place. split_projection ( self . tcx )
374+ {
374375 let base_ty = base_place. ty ( self . mir , self . tcx ) . to_ty ( self . tcx ) ;
375376 is_closure_or_generator ( base_ty)
376377 && self . mir . upvar_decls [ upvar_index. index ( ) ] . by_ref
@@ -391,17 +392,20 @@ fn suggest_ampmut_self<'cx, 'gcx, 'tcx>(
391392 local_decl : & mir:: LocalDecl < ' tcx > ,
392393) -> ( Span , String ) {
393394 let sp = local_decl. source_info . span ;
394- ( sp, match tcx. sess . codemap ( ) . span_to_snippet ( sp) {
395- Ok ( snippet) => {
396- let lt_pos = snippet. find ( '\'' ) ;
397- if let Some ( lt_pos) = lt_pos {
398- format ! ( "&{}mut self" , & snippet[ lt_pos..snippet. len( ) - 4 ] )
399- } else {
400- "&mut self" . to_string ( )
395+ (
396+ sp,
397+ match tcx. sess . codemap ( ) . span_to_snippet ( sp) {
398+ Ok ( snippet) => {
399+ let lt_pos = snippet. find ( '\'' ) ;
400+ if let Some ( lt_pos) = lt_pos {
401+ format ! ( "&{}mut self" , & snippet[ lt_pos..snippet. len( ) - 4 ] )
402+ } else {
403+ "&mut self" . to_string ( )
404+ }
401405 }
402- }
403- _ => "&mut self" . to_string ( )
404- } )
406+ _ => "&mut self" . to_string ( ) ,
407+ } ,
408+ )
405409}
406410
407411// When we want to suggest a user change a local variable to be a `&mut`, there
@@ -467,12 +471,14 @@ fn suggest_ampmut<'cx, 'gcx, 'tcx>(
467471
468472 let ty_mut = local_decl. ty . builtin_deref ( true ) . unwrap ( ) ;
469473 assert_eq ! ( ty_mut. mutbl, hir:: MutImmutable ) ;
470- ( highlight_span,
471- if local_decl. ty . is_region_ptr ( ) {
472- format ! ( "&mut {}" , ty_mut. ty)
473- } else {
474- format ! ( "*mut {}" , ty_mut. ty)
475- } )
474+ (
475+ highlight_span,
476+ if local_decl. ty . is_region_ptr ( ) {
477+ format ! ( "&mut {}" , ty_mut. ty)
478+ } else {
479+ format ! ( "*mut {}" , ty_mut. ty)
480+ } ,
481+ )
476482}
477483
478484fn is_closure_or_generator ( ty : ty:: Ty ) -> bool {
0 commit comments