@@ -2906,15 +2906,63 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
29062906 owned_sugg = true ;
29072907 }
29082908 if let Some ( ty) = lt_finder. found {
2909- if let TyKind :: Path ( None , Path { segments, .. } ) = & ty. kind
2909+ if let TyKind :: Path ( None , path @ Path { segments, .. } ) = & ty. kind
29102910 && segments. len ( ) == 1
2911- && segments[ 0 ] . ident . name == sym:: str
29122911 {
2913- // Don't suggest `-> str`, suggest `-> String`.
2914- sugg = vec ! [
2915- ( lt. span. with_hi( ty. span. hi( ) ) , "String" . to_string( ) ) ,
2916- ] ;
2917- } else if let TyKind :: Slice ( inner_ty) = & ty. kind {
2912+ if segments[ 0 ] . ident . name == sym:: str {
2913+ // Don't suggest `-> str`, suggest `-> String`.
2914+ sugg = vec ! [
2915+ ( lt. span. with_hi( ty. span. hi( ) ) , "String" . to_string( ) ) ,
2916+ ] ;
2917+ } else {
2918+ // Check if the path being borrowed is likely to be owned.
2919+ let path: Vec < _ > = Segment :: from_path ( path) ;
2920+ match self . resolve_path ( & path, Some ( TypeNS ) , None ) {
2921+ PathResult :: Module (
2922+ ModuleOrUniformRoot :: Module ( module) ,
2923+ ) => {
2924+ match module. res ( ) {
2925+ Some ( Res :: PrimTy ( ..) ) => { }
2926+ Some ( Res :: Def (
2927+ DefKind :: Struct
2928+ | DefKind :: Union
2929+ | DefKind :: Enum
2930+ | DefKind :: ForeignTy
2931+ | DefKind :: AssocTy
2932+ | DefKind :: OpaqueTy
2933+ | DefKind :: TyParam ,
2934+ _,
2935+ ) ) => { }
2936+ _ => { // Do not suggest in all other cases.
2937+ owned_sugg = false ;
2938+ }
2939+ }
2940+ }
2941+ PathResult :: NonModule ( res) => {
2942+ match res. base_res ( ) {
2943+ Res :: PrimTy ( ..) => { }
2944+ Res :: Def (
2945+ DefKind :: Struct
2946+ | DefKind :: Union
2947+ | DefKind :: Enum
2948+ | DefKind :: ForeignTy
2949+ | DefKind :: AssocTy
2950+ | DefKind :: OpaqueTy
2951+ | DefKind :: TyParam ,
2952+ _,
2953+ ) => { }
2954+ _ => { // Do not suggest in all other cases.
2955+ owned_sugg = false ;
2956+ }
2957+ }
2958+ }
2959+ _ => { // Do not suggest in all other cases.
2960+ owned_sugg = false ;
2961+ }
2962+ }
2963+ }
2964+ }
2965+ if let TyKind :: Slice ( inner_ty) = & ty. kind {
29182966 // Don't suggest `-> [T]`, suggest `-> Vec<T>`.
29192967 sugg = vec ! [
29202968 ( lt. span. with_hi( inner_ty. span. lo( ) ) , "Vec<" . to_string( ) ) ,
0 commit comments