@@ -4,7 +4,7 @@ use rustc_middle::ty;
44use rustc_mir_dataflow:: move_paths:: {
55 IllegalMoveOrigin , IllegalMoveOriginKind , LookupResult , MoveError , MovePathIndex ,
66} ;
7- use rustc_span:: Span ;
7+ use rustc_span:: { BytePos , Span } ;
88
99use crate :: diagnostics:: { DescribePlaceOpt , UseSpans } ;
1010use crate :: prefixes:: PrefixSet ;
@@ -148,7 +148,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
148148 match_span : Span ,
149149 statement_span : Span ,
150150 ) {
151- debug ! ( "append_binding_error(match_place={:?}, match_span={:?})" , match_place , match_span ) ;
151+ debug ! ( ?match_place , ?match_span , "append_binding_error" ) ;
152152
153153 let from_simple_let = match_place. is_none ( ) ;
154154 let match_place = match_place. unwrap_or ( move_from) ;
@@ -160,7 +160,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
160160 if let GroupedMoveError :: MovesFromPlace { span, binds_to, .. } = ge
161161 && match_span == * span
162162 {
163- debug ! ( "appending local({:?}) to list" , bind_to ) ;
163+ debug ! ( "appending local({bind_to :?}) to list" ) ;
164164 if !binds_to. is_empty ( ) {
165165 binds_to. push ( bind_to) ;
166166 }
@@ -198,7 +198,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
198198 } = ge
199199 {
200200 if match_span == * span && mpi == * other_mpi {
201- debug ! ( "appending local({:?}) to list" , bind_to ) ;
201+ debug ! ( "appending local({bind_to :?}) to list" ) ;
202202 binds_to. push ( bind_to) ;
203203 return ;
204204 }
@@ -410,14 +410,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
410410 fn add_move_hints ( & self , error : GroupedMoveError < ' tcx > , err : & mut Diagnostic , span : Span ) {
411411 match error {
412412 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+ ) ;
421419
422420 if binds_to. is_empty ( ) {
423421 let place_ty = move_from. ty ( self . body , self . infcx . tcx ) . ty ;
@@ -469,28 +467,36 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
469467 VarBindingForm { pat_span, .. } ,
470468 ) ) ) ) = bind_to. local_info
471469 {
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)
473476 {
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 ( ) ) ) ;
486492 }
487493 }
488494 suggestions. sort_unstable_by_key ( |& ( span, _, _) | span) ;
489495 suggestions. dedup_by_key ( |& mut ( span, _, _) | span) ;
490496 for ( span, to_remove, suggestion) in suggestions {
491- err. span_suggestion (
497+ err. span_suggestion_verbose (
492498 span,
493- & format ! ( "consider removing the ` {to_remove}` " ) ,
499+ & format ! ( "consider removing the {to_remove}" ) ,
494500 suggestion,
495501 Applicability :: MachineApplicable ,
496502 ) ;
@@ -521,8 +527,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
521527
522528 if binds_to. len ( ) > 1 {
523529 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",
526532 ) ;
527533 }
528534 }
0 commit comments