@@ -3105,15 +3105,63 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
31053105 owned_sugg = true ;
31063106 }
31073107 if let Some ( ty) = lt_finder. found {
3108- if let TyKind :: Path ( None , Path { segments, .. } ) = & ty. kind
3108+ if let TyKind :: Path ( None , path @ Path { segments, .. } ) = & ty. kind
31093109 && segments. len ( ) == 1
3110- && segments[ 0 ] . ident . name == sym:: str
31113110 {
3112- // Don't suggest `-> str`, suggest `-> String`.
3113- sugg = vec ! [
3114- ( lt. span. with_hi( ty. span. hi( ) ) , "String" . to_string( ) ) ,
3115- ] ;
3116- } else if let TyKind :: Slice ( inner_ty) = & ty. kind {
3111+ if segments[ 0 ] . ident . name == sym:: str {
3112+ // Don't suggest `-> str`, suggest `-> String`.
3113+ sugg = vec ! [
3114+ ( lt. span. with_hi( ty. span. hi( ) ) , "String" . to_string( ) ) ,
3115+ ] ;
3116+ } else {
3117+ // Check if the path being borrowed is likely to be owned.
3118+ let path: Vec < _ > = Segment :: from_path ( path) ;
3119+ match self . resolve_path ( & path, Some ( TypeNS ) , None ) {
3120+ PathResult :: Module (
3121+ ModuleOrUniformRoot :: Module ( module) ,
3122+ ) => {
3123+ match module. res ( ) {
3124+ Some ( Res :: PrimTy ( ..) ) => { }
3125+ Some ( Res :: Def (
3126+ DefKind :: Struct
3127+ | DefKind :: Union
3128+ | DefKind :: Enum
3129+ | DefKind :: ForeignTy
3130+ | DefKind :: AssocTy
3131+ | DefKind :: OpaqueTy
3132+ | DefKind :: TyParam ,
3133+ _,
3134+ ) ) => { }
3135+ _ => { // Do not suggest in all other cases.
3136+ owned_sugg = false ;
3137+ }
3138+ }
3139+ }
3140+ PathResult :: NonModule ( res) => {
3141+ match res. base_res ( ) {
3142+ Res :: PrimTy ( ..) => { }
3143+ Res :: Def (
3144+ DefKind :: Struct
3145+ | DefKind :: Union
3146+ | DefKind :: Enum
3147+ | DefKind :: ForeignTy
3148+ | DefKind :: AssocTy
3149+ | DefKind :: OpaqueTy
3150+ | DefKind :: TyParam ,
3151+ _,
3152+ ) => { }
3153+ _ => { // Do not suggest in all other cases.
3154+ owned_sugg = false ;
3155+ }
3156+ }
3157+ }
3158+ _ => { // Do not suggest in all other cases.
3159+ owned_sugg = false ;
3160+ }
3161+ }
3162+ }
3163+ }
3164+ if let TyKind :: Slice ( inner_ty) = & ty. kind {
31173165 // Don't suggest `-> [T]`, suggest `-> Vec<T>`.
31183166 sugg = vec ! [
31193167 ( lt. span. with_hi( inner_ty. span. lo( ) ) , "Vec<" . to_string( ) ) ,
0 commit comments