@@ -413,7 +413,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
413
413
}
414
414
}
415
415
416
- // Also suggest adding mut for upvars
416
+ // Also suggest adding mut for upvars.
417
417
PlaceRef {
418
418
local,
419
419
projection : [ proj_base @ .., ProjectionElem :: Field ( upvar_index, _) ] ,
@@ -467,9 +467,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
467
467
}
468
468
}
469
469
470
- // complete hack to approximate old AST-borrowck
471
- // diagnostic: if the span starts with a mutable borrow of
472
- // a local variable, then just suggest the user remove it.
470
+ // Complete hack to approximate old AST-borrowck diagnostic: if the span starts
471
+ // with a mutable borrow of a local variable, then just suggest the user remove it.
473
472
PlaceRef { local : _, projection : [ ] }
474
473
if self
475
474
. infcx
@@ -798,7 +797,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
798
797
) ;
799
798
}
800
799
801
- // point to span of upvar making closure call require mutable borrow
800
+ // Point to span of upvar making closure call that requires a mutable borrow
802
801
fn show_mutating_upvar (
803
802
& self ,
804
803
tcx : TyCtxt < ' _ > ,
@@ -854,7 +853,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
854
853
} else {
855
854
bug ! ( "not an upvar" )
856
855
} ;
857
- // sometimes we deliberately don't store the name of a place when coming from a macro in
856
+ // Sometimes we deliberately don't store the name of a place when coming from a macro in
858
857
// another crate. We generally want to limit those diagnostics a little, to hide
859
858
// implementation details (such as those from pin!() or format!()). In that case show a
860
859
// slightly different error message, or none at all if something else happened. In other
@@ -965,8 +964,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
965
964
let def_id = tcx. hir_enclosing_body_owner ( fn_call_id) ;
966
965
let mut look_at_return = true ;
967
966
968
- // If the HIR node is a function or method call gets the def ID
969
- // of the called function or method and the span and args of the call expr
967
+ // If the HIR node is a function or method call, get the DefId
968
+ // of the callee function or method, the span, and args of the call expr
970
969
let get_call_details = || {
971
970
let hir:: Node :: Expr ( hir:: Expr { hir_id, kind, .. } ) = node else {
972
971
return None ;
@@ -1080,7 +1079,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
1080
1079
let mut cur_expr = expr;
1081
1080
while let ExprKind :: MethodCall ( path_segment, recv, _, _) = cur_expr. kind {
1082
1081
if path_segment. ident . name == sym:: iter {
1083
- // check `_ty` has `iter_mut` method
1082
+ // Check that the type has an `iter_mut` method.
1084
1083
let res = self
1085
1084
. infcx
1086
1085
. tcx
@@ -1119,7 +1118,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
1119
1118
let ( is_trait_sig, is_local, local_trait) = self . is_error_in_trait ( local) ;
1120
1119
1121
1120
if is_trait_sig && !is_local {
1122
- // Do not suggest to change the signature when the trait comes from another crate.
1121
+ // Do not suggest changing the signature when the trait comes from another crate.
1123
1122
err. span_label (
1124
1123
local_decl. source_info . span ,
1125
1124
format ! ( "this is an immutable {pointer_desc}" ) ,
@@ -1140,7 +1139,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
1140
1139
opt_ty_info,
1141
1140
..
1142
1141
} ) ) => {
1143
- // check if the RHS is from desugaring
1142
+ // Check if the RHS is from desugaring.
1144
1143
let first_assignment = find_assignments ( & self . body , local) . first ( ) . copied ( ) ;
1145
1144
let first_assignment_stmt = first_assignment
1146
1145
. and_then ( |loc| self . body [ loc. block ] . statements . get ( loc. statement_index ) ) ;
@@ -1160,7 +1159,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
1160
1159
// it with does not, so use the local_span for our checks later.
1161
1160
source_span = Some ( local_span) ;
1162
1161
if let Some ( DesugaringKind :: ForLoop ) = local_span. desugaring_kind ( ) {
1163
- // on for loops, RHS points to the iterator part
1162
+ // On for loops, RHS points to the iterator part.
1164
1163
self . suggest_similar_mut_method_for_for_loop ( err, local_span) ;
1165
1164
err. span_label (
1166
1165
local_span,
@@ -1170,14 +1169,14 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
1170
1169
}
1171
1170
}
1172
1171
1173
- // don 't create labels for compiler-generated spans or spans not from users' code
1172
+ // Don 't create labels for compiler-generated spans or spans not from users' code.
1174
1173
if source_span. is_some_and ( |s| {
1175
1174
s. desugaring_kind ( ) . is_some ( ) || self . infcx . tcx . sess . source_map ( ) . is_imported ( s)
1176
1175
} ) {
1177
1176
return ;
1178
1177
}
1179
1178
1180
- // could be because we're in an `async fn`
1179
+ // This could be because we're in an `async fn`.
1181
1180
if name == kw:: SelfLower && opt_ty_info. is_none ( ) {
1182
1181
let ( span, suggestion) = suggest_ampmut_self ( self . infcx . tcx , decl_span) ;
1183
1182
( AmpMutSugg :: Type { span, suggestion, additional : None } , None )
@@ -1249,16 +1248,16 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
1249
1248
AmpMutSugg :: ChangeBinding => ( vec ! [ ] , true ) ,
1250
1249
} ;
1251
1250
1252
- // find a binding's type to make mutable.
1251
+ // Find a binding's type to make mutable.
1253
1252
let ( binding_exists, span) = match local_var_ty_info {
1254
- // if this is a variable binding with an explicit type,
1253
+ // If this is a variable binding with an explicit type,
1255
1254
// then we will suggest changing it to be mutable.
1256
- // this is `Applicability::MachineApplicable`.
1255
+ // This is `Applicability::MachineApplicable`.
1257
1256
Some ( ty_span) => ( true , ty_span) ,
1258
1257
1259
- // otherwise , we'll suggest *adding* an annotated type, we'll suggest
1258
+ // Otherwise , we'll suggest *adding* an annotated type, we'll suggest
1260
1259
// the RHS's type for that.
1261
- // this is `Applicability::HasPlaceholders`.
1260
+ // This is `Applicability::HasPlaceholders`.
1262
1261
None => ( false , decl_span) ,
1263
1262
} ;
1264
1263
@@ -1267,31 +1266,31 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
1267
1266
return ;
1268
1267
}
1269
1268
1270
- // if the binding already exists and is a reference with an explicit
1271
- // lifetime, then we can suggest adding ` mut`. this is special-cased from
1269
+ // If the binding already exists and is a reference with an explicit
1270
+ // lifetime, then we can suggest adding ` mut`. This is special-cased from
1272
1271
// the path without an explicit lifetime.
1273
1272
let ( sugg_span, sugg_str, suggest_now) = if let Ok ( src) = self . infcx . tcx . sess . source_map ( ) . span_to_snippet ( span)
1274
1273
&& src. starts_with ( "&'" )
1275
- // note that `&' a T` is invalid so this is correct.
1274
+ // Note that `&' a T` is invalid so this is correct.
1276
1275
&& let Some ( ws_pos) = src. find ( char:: is_whitespace)
1277
1276
{
1278
1277
let span = span. with_lo ( span. lo ( ) + BytePos ( ws_pos as u32 ) ) . shrink_to_lo ( ) ;
1279
1278
( span, " mut" . to_owned ( ) , true )
1280
- // if there is already a binding, we modify it to be `mut`
1279
+ // If there is already a binding, we modify it to be `mut`.
1281
1280
} else if binding_exists {
1282
- // shrink the span to just after the `&` in `&variable`
1281
+ // Shrink the span to just after the `&` in `&variable`.
1283
1282
let span = span. with_lo ( span. lo ( ) + BytePos ( 1 ) ) . shrink_to_lo ( ) ;
1284
1283
( span, "mut " . to_owned ( ) , true )
1285
1284
} else {
1286
- // otherwise , suggest that the user annotates the binding; we provide the
1285
+ // Otherwise , suggest that the user annotates the binding; We provide the
1287
1286
// type of the local.
1288
1287
let ty = local_decl. ty . builtin_deref ( true ) . unwrap ( ) ;
1289
1288
1290
1289
( span, format ! ( "{}mut {}" , if local_decl. ty. is_ref( ) { "&" } else { "*" } , ty) , false )
1291
1290
} ;
1292
1291
1293
1292
if suggest_now {
1294
- // suggest changing `&x` to `&mut x` and changing `&T` to `&mut T` at the same time
1293
+ // Suggest changing `&x` to `&mut x` and changing `&T` to `&mut T` at the same time.
1295
1294
let has_change = !sugg. is_empty ( ) ;
1296
1295
sugg. push ( ( sugg_span, sugg_str) ) ;
1297
1296
suggest (
@@ -1535,9 +1534,9 @@ fn suggest_ampmut<'tcx>(
1535
1534
opt_assignment_rhs_stmt : Option < & Statement < ' tcx > > ,
1536
1535
) -> Option < AmpMutSugg > {
1537
1536
let tcx = infcx. tcx ;
1538
- // if there is a RHS and it starts with a `&` from it, then check if it is
1537
+ // If there is a RHS and it starts with a `&` from it, then check if it is
1539
1538
// mutable, and if not, put suggest putting `mut ` to make it mutable.
1540
- // we don't have to worry about lifetime annotations here because they are
1539
+ // We don't have to worry about lifetime annotations here because they are
1541
1540
// not valid when taking a reference. For example, the following is not valid Rust:
1542
1541
//
1543
1542
// let x: &i32 = &'a 5;
@@ -1550,15 +1549,15 @@ fn suggest_ampmut<'tcx>(
1550
1549
{
1551
1550
let mut rvalue = rvalue;
1552
1551
1553
- // take some special care when handling `let _x = &*_y`:
1554
- // we want to know if this is part of an overloaded index, so `let x = &a[0]`,
1555
- // or whether this is a usertype ascription (`let _x: &T = y`)
1552
+ // Take some special care when handling `let _x = &*_y`:
1553
+ // We want to know if this is part of an overloaded index, so `let x = &a[0]`,
1554
+ // or whether this is a usertype ascription (`let _x: &T = y`).
1556
1555
if let Rvalue :: Ref ( _, BorrowKind :: Shared , place) = rvalue
1557
1556
&& place. projection . len ( ) == 1
1558
1557
&& place. projection [ 0 ] == ProjectionElem :: Deref
1559
1558
&& let Some ( assign) = find_assignments ( & body, place. local ) . first ( )
1560
1559
{
1561
- // if this is a usertype ascription (`let _x: &T = _y`) then pierce through it as either we want
1560
+ // If this is a usertype ascription (`let _x: &T = _y`) then pierce through it as either we want
1562
1561
// to suggest `&mut` on the expression (handled here) or we return `None` and let the caller
1563
1562
// suggest `&mut` on the type if the expression seems fine (e.g. `let _x: &T = &mut _y`).
1564
1563
if let Some ( user_ty_projs) = body. local_decls [ lhs. local ] . user_ty . as_ref ( )
@@ -1585,12 +1584,12 @@ fn suggest_ampmut<'tcx>(
1585
1584
tcx. require_lang_item ( hir:: LangItem :: IndexMut , rhs_span) ,
1586
1585
method_args,
1587
1586
) ;
1588
- // the type only implements `Index` but not `IndexMut`, we must not suggest `&mut`.
1587
+ // The type only implements `Index` but not `IndexMut`, we must not suggest `&mut`.
1589
1588
if !infcx
1590
1589
. type_implements_trait ( trait_ref. def_id , trait_ref. args , infcx. param_env )
1591
1590
. must_apply_considering_regions ( )
1592
1591
{
1593
- // suggest `get_mut` if type is a `BTreeMap` or `HashMap`.
1592
+ // Suggest `get_mut` if type is a `BTreeMap` or `HashMap`.
1594
1593
if let ty:: Adt ( def, _) = trait_ref. self_ty ( ) . kind ( )
1595
1594
&& [ sym:: BTreeMap , sym:: HashMap ]
1596
1595
. into_iter ( )
@@ -1610,7 +1609,7 @@ fn suggest_ampmut<'tcx>(
1610
1609
1611
1610
let sugg = match rvalue {
1612
1611
Rvalue :: Ref ( _, BorrowKind :: Shared , _) if let Some ( ref_idx) = rhs_str. find ( '&' ) => {
1613
- // shrink the span to just after the `&` in `&variable`
1612
+ // Shrink the span to just after the `&` in `&variable`.
1614
1613
Some ( (
1615
1614
rhs_span. with_lo ( rhs_span. lo ( ) + BytePos ( ref_idx as u32 + 1 ) ) . shrink_to_lo ( ) ,
1616
1615
"mut " . to_owned ( ) ,
0 commit comments