@@ -380,16 +380,14 @@ impl<'a, 'tcx> CastCheck<'tcx> {
380380                err. span_label ( self . span ,  "invalid cast" ) ; 
381381                if  self . expr_ty . is_numeric ( )  { 
382382                    if  self . expr_ty  == fcx. tcx . types . u32  { 
383-                         match  fcx. tcx . sess . source_map ( ) . span_to_snippet ( self . expr . span )  { 
384-                             Ok ( snippet)  => err. span_suggestion ( 
385-                                 self . span , 
386-                                 "try `char::from_u32` instead" , 
387-                                 format ! ( "char::from_u32({snippet})" ) , 
388-                                 Applicability :: MachineApplicable , 
389-                             ) , 
390- 
391-                             Err ( _)  => err. span_help ( self . span ,  "try `char::from_u32` instead" ) , 
392-                         } ; 
383+                         err. multipart_suggestion ( 
384+                             "try `char::from_u32` instead" , 
385+                             vec ! [ 
386+                                 ( self . expr_span. shrink_to_lo( ) ,  "char::from_u32(" . to_string( ) ) , 
387+                                 ( self . expr_span. shrink_to_hi( ) . to( self . cast_span) ,  ")" . to_string( ) ) , 
388+                             ] , 
389+                             Applicability :: MachineApplicable , 
390+                         ) ; 
393391                    }  else  if  self . expr_ty  == fcx. tcx . types . i8  { 
394392                        err. span_help ( self . span ,  "try casting from `u8` instead" ) ; 
395393                    }  else  { 
@@ -496,9 +494,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
496494                )  { 
497495                    let  mut  label = true ; 
498496                    // Check `impl From<self.expr_ty> for self.cast_ty {}` for accurate suggestion: 
499-                     if  let  Ok ( snippet)  = fcx. tcx . sess . source_map ( ) . span_to_snippet ( self . expr_span ) 
500-                         && let  Some ( from_trait)  = fcx. tcx . get_diagnostic_item ( sym:: From ) 
501-                     { 
497+                     if  let  Some ( from_trait)  = fcx. tcx . get_diagnostic_item ( sym:: From )  { 
502498                        let  ty = fcx. resolve_vars_if_possible ( self . cast_ty ) ; 
503499                        let  expr_ty = fcx. resolve_vars_if_possible ( self . expr_ty ) ; 
504500                        if  fcx
@@ -507,25 +503,22 @@ impl<'a, 'tcx> CastCheck<'tcx> {
507503                            . must_apply_modulo_regions ( ) 
508504                        { 
509505                            label = false ; 
510-                             if  let  ty:: Adt ( def,  args)  = self . cast_ty . kind ( )  { 
511-                                 err. span_suggestion_verbose ( 
512-                                     self . span , 
513-                                     "consider using the `From` trait instead" , 
514-                                     format ! ( 
515-                                         "{}::from({})" , 
516-                                         fcx. tcx. value_path_str_with_args( def. did( ) ,  args) , 
517-                                         snippet
518-                                     ) , 
519-                                     Applicability :: MaybeIncorrect , 
520-                                 ) ; 
506+                             let  to_ty = if  let  ty:: Adt ( def,  args)  = self . cast_ty . kind ( )  { 
507+                                 fcx. tcx . value_path_str_with_args ( def. did ( ) ,  args) 
521508                            }  else  { 
522-                                 err. span_suggestion ( 
523-                                     self . span , 
524-                                     "consider using the `From` trait instead" , 
525-                                     format ! ( "{}::from({})" ,  self . cast_ty,  snippet) , 
526-                                     Applicability :: MaybeIncorrect , 
527-                                 ) ; 
509+                                 self . cast_ty . to_string ( ) 
528510                            } ; 
511+                             err. multipart_suggestion ( 
512+                                 "consider using the `From` trait instead" , 
513+                                 vec ! [ 
514+                                     ( self . expr_span. shrink_to_lo( ) ,  format!( "{to_ty}::from(" ) ) , 
515+                                     ( 
516+                                         self . expr_span. shrink_to_hi( ) . to( self . cast_span) , 
517+                                         ")" . to_string( ) , 
518+                                     ) , 
519+                                 ] , 
520+                                 Applicability :: MaybeIncorrect , 
521+                             ) ; 
529522                        } 
530523                    } 
531524
@@ -654,38 +647,22 @@ impl<'a, 'tcx> CastCheck<'tcx> {
654647        match  self . expr_ty . kind ( )  { 
655648            ty:: Ref ( _,  _,  mt)  => { 
656649                let  mtstr = mt. prefix_str ( ) ; 
657-                 match  fcx. tcx . sess . source_map ( ) . span_to_snippet ( self . cast_span )  { 
658-                     Ok ( s)  => { 
659-                         err. span_suggestion ( 
660-                             self . cast_span , 
661-                             "try casting to a reference instead" , 
662-                             format ! ( "&{mtstr}{s}" ) , 
663-                             Applicability :: MachineApplicable , 
664-                         ) ; 
665-                     } 
666-                     Err ( _)  => { 
667-                         let  msg = format ! ( "did you mean `&{mtstr}{tstr}`?" ) ; 
668-                         err. span_help ( self . cast_span ,  msg) ; 
669-                     } 
670-                 } 
650+                 err. span_suggestion_verbose ( 
651+                     self . cast_span . shrink_to_lo ( ) , 
652+                     "try casting to a reference instead" , 
653+                     format ! ( "&{mtstr}" ) , 
654+                     Applicability :: MachineApplicable , 
655+                 ) ; 
671656            } 
672657            ty:: Adt ( def,  ..)  if  def. is_box ( )  => { 
673-                 match  fcx. tcx . sess . source_map ( ) . span_to_snippet ( self . cast_span )  { 
674-                     Ok ( s)  => { 
675-                         err. span_suggestion ( 
676-                             self . cast_span , 
677-                             "you can cast to a `Box` instead" , 
678-                             format ! ( "Box<{s}>" ) , 
679-                             Applicability :: MachineApplicable , 
680-                         ) ; 
681-                     } 
682-                     Err ( _)  => { 
683-                         err. span_help ( 
684-                             self . cast_span , 
685-                             format ! ( "you might have meant `Box<{tstr}>`" ) , 
686-                         ) ; 
687-                     } 
688-                 } 
658+                 err. multipart_suggestion ( 
659+                     "you can cast to a `Box` instead" , 
660+                     vec ! [ 
661+                         ( self . cast_span. shrink_to_lo( ) ,  "Box<" . to_string( ) ) , 
662+                         ( self . cast_span. shrink_to_hi( ) ,  ">" . to_string( ) ) , 
663+                     ] , 
664+                     Applicability :: MachineApplicable , 
665+                 ) ; 
689666            } 
690667            _ => { 
691668                err. span_help ( self . expr_span ,  "consider using a box or reference as appropriate" ) ; 
0 commit comments