@@ -4,7 +4,7 @@ use rustc_middle::ty;
4
4
use rustc_mir_dataflow:: move_paths:: {
5
5
IllegalMoveOrigin , IllegalMoveOriginKind , LookupResult , MoveError , MovePathIndex ,
6
6
} ;
7
- use rustc_span:: Span ;
7
+ use rustc_span:: { BytePos , Span } ;
8
8
9
9
use crate :: diagnostics:: { DescribePlaceOpt , UseSpans } ;
10
10
use crate :: prefixes:: PrefixSet ;
@@ -148,7 +148,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
148
148
match_span : Span ,
149
149
statement_span : Span ,
150
150
) {
151
- debug ! ( "append_binding_error(match_place={:?}, match_span={:?})" , match_place , match_span ) ;
151
+ debug ! ( ?match_place , ?match_span , "append_binding_error" ) ;
152
152
153
153
let from_simple_let = match_place. is_none ( ) ;
154
154
let match_place = match_place. unwrap_or ( move_from) ;
@@ -160,7 +160,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
160
160
if let GroupedMoveError :: MovesFromPlace { span, binds_to, .. } = ge
161
161
&& match_span == * span
162
162
{
163
- debug ! ( "appending local({:?}) to list" , bind_to ) ;
163
+ debug ! ( "appending local({bind_to :?}) to list" ) ;
164
164
if !binds_to. is_empty ( ) {
165
165
binds_to. push ( bind_to) ;
166
166
}
@@ -198,7 +198,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
198
198
} = ge
199
199
{
200
200
if match_span == * span && mpi == * other_mpi {
201
- debug ! ( "appending local({:?}) to list" , bind_to ) ;
201
+ debug ! ( "appending local({bind_to :?}) to list" ) ;
202
202
binds_to. push ( bind_to) ;
203
203
return ;
204
204
}
@@ -410,14 +410,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
410
410
fn add_move_hints ( & self , error : GroupedMoveError < ' tcx > , err : & mut Diagnostic , span : Span ) {
411
411
match error {
412
412
GroupedMoveError :: MovesFromPlace { mut binds_to, move_from, .. } => {
413
- if let Ok ( snippet) = self . infcx . tcx . sess . source_map ( ) . span_to_snippet ( span) {
414
- err. span_suggestion (
415
- span,
416
- "consider borrowing here" ,
417
- format ! ( "&{snippet}" ) ,
418
- Applicability :: Unspecified ,
419
- ) ;
420
- }
413
+ err. span_suggestion_verbose (
414
+ span. shrink_to_lo ( ) ,
415
+ "consider borrowing here" ,
416
+ "&" . to_string ( ) ,
417
+ Applicability :: Unspecified ,
418
+ ) ;
421
419
422
420
if binds_to. is_empty ( ) {
423
421
let place_ty = move_from. ty ( self . body , self . infcx . tcx ) . ty ;
@@ -469,28 +467,36 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
469
467
VarBindingForm { pat_span, .. } ,
470
468
) ) ) ) = bind_to. local_info
471
469
{
472
- if let Ok ( pat_snippet) = self . infcx . tcx . sess . source_map ( ) . span_to_snippet ( pat_span)
470
+ let Ok ( pat_snippet) =
471
+ self . infcx . tcx . sess . source_map ( ) . span_to_snippet ( pat_span) else { continue ; } ;
472
+ let Some ( stripped) = pat_snippet. strip_prefix ( '&' ) else { continue ; } ;
473
+ let inner_pat_snippet = stripped. trim_start ( ) ;
474
+ let ( pat_span, suggestion, to_remove) = if inner_pat_snippet. starts_with ( "mut" )
475
+ && inner_pat_snippet[ "mut" . len ( ) ..] . starts_with ( rustc_lexer:: is_whitespace)
473
476
{
474
- if let Some ( stripped) = pat_snippet. strip_prefix ( '&' ) {
475
- let pat_snippet = stripped. trim_start ( ) ;
476
- let ( suggestion, to_remove) = if pat_snippet. starts_with ( "mut" )
477
- && pat_snippet[ "mut" . len ( ) ..] . starts_with ( rustc_lexer:: is_whitespace)
478
- {
479
- ( pat_snippet[ "mut" . len ( ) ..] . trim_start ( ) , "&mut" )
480
- } else {
481
- ( pat_snippet, "&" )
482
- } ;
483
- suggestions. push ( ( pat_span, to_remove, suggestion. to_owned ( ) ) ) ;
484
- }
485
- }
477
+ let pat_span = pat_span. with_hi (
478
+ pat_span. lo ( )
479
+ + BytePos ( ( pat_snippet. len ( ) - inner_pat_snippet. len ( ) ) as u32 ) ,
480
+ ) ;
481
+ ( pat_span, String :: new ( ) , "mutable borrow" )
482
+ } else {
483
+ let pat_span = pat_span. with_hi (
484
+ pat_span. lo ( )
485
+ + BytePos (
486
+ ( pat_snippet. len ( ) - inner_pat_snippet. trim_start ( ) . len ( ) ) as u32 ,
487
+ ) ,
488
+ ) ;
489
+ ( pat_span, String :: new ( ) , "borrow" )
490
+ } ;
491
+ suggestions. push ( ( pat_span, to_remove, suggestion. to_owned ( ) ) ) ;
486
492
}
487
493
}
488
494
suggestions. sort_unstable_by_key ( |& ( span, _, _) | span) ;
489
495
suggestions. dedup_by_key ( |& mut ( span, _, _) | span) ;
490
496
for ( span, to_remove, suggestion) in suggestions {
491
- err. span_suggestion (
497
+ err. span_suggestion_verbose (
492
498
span,
493
- & format ! ( "consider removing the ` {to_remove}` " ) ,
499
+ & format ! ( "consider removing the {to_remove}" ) ,
494
500
suggestion,
495
501
Applicability :: MachineApplicable ,
496
502
) ;
@@ -521,8 +527,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
521
527
522
528
if binds_to. len ( ) > 1 {
523
529
err. note (
524
- "move occurs because these variables have types that \
525
- don't implement the `Copy` trait",
530
+ "move occurs because these variables have types that don't implement the `Copy` \
531
+ trait",
526
532
) ;
527
533
}
528
534
}
0 commit comments