@@ -460,7 +460,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
460460 }
461461
462462 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 ( ) ;
464464 for local in binds_to {
465465 let bind_to = & self . body . local_decls [ * local] ;
466466 if let Some ( box LocalInfo :: User ( ClearCrossCrate :: Set ( BindingForm :: Var (
@@ -469,7 +469,14 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
469469 {
470470 let Ok ( pat_snippet) =
471471 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+ } ;
473480 let inner_pat_snippet = stripped. trim_start ( ) ;
474481 let ( pat_span, suggestion, to_remove) = if inner_pat_snippet. starts_with ( "mut" )
475482 && inner_pat_snippet[ "mut" . len ( ) ..] . starts_with ( rustc_lexer:: is_whitespace)
@@ -488,18 +495,17 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
488495 ) ;
489496 ( pat_span, String :: new ( ) , "borrow" )
490497 } ;
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+ ) ) ;
492503 }
493504 }
494505 suggestions. sort_unstable_by_key ( |& ( span, _, _) | span) ;
495506 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 ) ;
503509 }
504510 }
505511
0 commit comments