@@ -460,7 +460,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
460
460
}
461
461
462
462
fn add_move_error_suggestions ( & self , err : & mut Diagnostic , binds_to : & [ Local ] ) {
463
- let mut suggestions: Vec < ( Span , & str , String ) > = Vec :: new ( ) ;
463
+ let mut suggestions: Vec < ( Span , String , String ) > = Vec :: new ( ) ;
464
464
for local in binds_to {
465
465
let bind_to = & self . body . local_decls [ * local] ;
466
466
if let Some ( box LocalInfo :: User ( ClearCrossCrate :: Set ( BindingForm :: Var (
@@ -469,7 +469,14 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
469
469
{
470
470
let Ok ( pat_snippet) =
471
471
self . infcx . tcx . sess . source_map ( ) . span_to_snippet ( pat_span) else { continue ; } ;
472
- let Some ( stripped) = pat_snippet. strip_prefix ( '&' ) else { continue ; } ;
472
+ let Some ( stripped) = pat_snippet. strip_prefix ( '&' ) else {
473
+ suggestions. push ( (
474
+ bind_to. source_info . span . shrink_to_lo ( ) ,
475
+ "consider borrowing the pattern binding" . to_string ( ) ,
476
+ "ref " . to_string ( ) ,
477
+ ) ) ;
478
+ continue ;
479
+ } ;
473
480
let inner_pat_snippet = stripped. trim_start ( ) ;
474
481
let ( pat_span, suggestion, to_remove) = if inner_pat_snippet. starts_with ( "mut" )
475
482
&& inner_pat_snippet[ "mut" . len ( ) ..] . starts_with ( rustc_lexer:: is_whitespace)
@@ -488,18 +495,17 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
488
495
) ;
489
496
( pat_span, String :: new ( ) , "borrow" )
490
497
} ;
491
- suggestions. push ( ( pat_span, to_remove, suggestion. to_owned ( ) ) ) ;
498
+ suggestions. push ( (
499
+ pat_span,
500
+ format ! ( "consider removing the {to_remove}" ) ,
501
+ suggestion. to_string ( ) ,
502
+ ) ) ;
492
503
}
493
504
}
494
505
suggestions. sort_unstable_by_key ( |& ( span, _, _) | span) ;
495
506
suggestions. dedup_by_key ( |& mut ( span, _, _) | span) ;
496
- for ( span, to_remove, suggestion) in suggestions {
497
- err. span_suggestion_verbose (
498
- span,
499
- & format ! ( "consider removing the {to_remove}" ) ,
500
- suggestion,
501
- Applicability :: MachineApplicable ,
502
- ) ;
507
+ for ( span, msg, suggestion) in suggestions {
508
+ err. span_suggestion_verbose ( span, & msg, suggestion, Applicability :: MachineApplicable ) ;
503
509
}
504
510
}
505
511
0 commit comments