@@ -2880,6 +2880,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
2880
2880
} else {
2881
2881
"instead, you are more likely to want"
2882
2882
} ;
2883
+ let mut owned_sugg = lt. kind == MissingLifetimeKind :: Ampersand ;
2883
2884
let mut sugg = vec ! [ ( lt. span, String :: new( ) ) ] ;
2884
2885
if let Some ( ( kind, _span) ) =
2885
2886
self . diagnostic_metadata . current_function
@@ -2893,6 +2894,17 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
2893
2894
} ;
2894
2895
lt_finder. visit_ty ( & ty) ;
2895
2896
2897
+ if let [ Ty { span, kind : TyKind :: Ref ( _, mut_ty) , ..} ]
2898
+ = & lt_finder. seen [ ..]
2899
+ {
2900
+ // We might have a situation like
2901
+ // fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()>
2902
+ // but `lt.span` only points at `'_`, so to suggest `-> Option<()>`
2903
+ // we need to find a more accurate span to end up with
2904
+ // fn g<'a>(mut x: impl Iterator<Item = &'_ ()>) -> Option<()>
2905
+ sugg = vec ! [ ( span. with_hi( mut_ty. ty. span. lo( ) ) , String :: new( ) ) ] ;
2906
+ owned_sugg = true ;
2907
+ }
2896
2908
if let Some ( ty) = lt_finder. found {
2897
2909
if let TyKind :: Path ( None , Path { segments, .. } ) = & ty. kind
2898
2910
&& segments. len ( ) == 1
@@ -2902,17 +2914,16 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
2902
2914
sugg = vec ! [
2903
2915
( lt. span. with_hi( ty. span. hi( ) ) , "String" . to_string( ) ) ,
2904
2916
] ;
2905
- }
2906
- if let TyKind :: Slice ( inner_ty) = & ty. kind {
2917
+ } else if let TyKind :: Slice ( inner_ty) = & ty. kind {
2907
2918
// Don't suggest `-> [T]`, suggest `-> Vec<T>`.
2908
2919
sugg = vec ! [
2909
2920
( lt. span. with_hi( inner_ty. span. lo( ) ) , "Vec<" . to_string( ) ) ,
2910
2921
( ty. span. with_lo( inner_ty. span. hi( ) ) , ">" . to_string( ) ) ,
2911
2922
] ;
2912
2923
}
2913
2924
}
2914
- } ;
2915
- if lt . kind == MissingLifetimeKind :: Ampersand {
2925
+ }
2926
+ if owned_sugg {
2916
2927
err. multipart_suggestion_verbose (
2917
2928
format ! ( "{pre} to return an owned value" ) ,
2918
2929
sugg,
0 commit comments