Account for '_ in lifetime suggestion
This commit is contained in:
parent
2a92d820c7
commit
85f26ade8d
3 changed files with 35 additions and 4 deletions
|
@ -3079,6 +3079,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
||||||
} else {
|
} else {
|
||||||
"instead, you are more likely to want"
|
"instead, you are more likely to want"
|
||||||
};
|
};
|
||||||
|
let mut owned_sugg = lt.kind == MissingLifetimeKind::Ampersand;
|
||||||
let mut sugg = vec![(lt.span, String::new())];
|
let mut sugg = vec![(lt.span, String::new())];
|
||||||
if let Some((kind, _span)) =
|
if let Some((kind, _span)) =
|
||||||
self.diagnostic_metadata.current_function
|
self.diagnostic_metadata.current_function
|
||||||
|
@ -3092,6 +3093,17 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
||||||
};
|
};
|
||||||
lt_finder.visit_ty(&ty);
|
lt_finder.visit_ty(&ty);
|
||||||
|
|
||||||
|
if let [Ty { span, kind: TyKind::Ref(_, mut_ty), ..}]
|
||||||
|
= <_finder.seen[..]
|
||||||
|
{
|
||||||
|
// We might have a situation like
|
||||||
|
// fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()>
|
||||||
|
// but `lt.span` only points at `'_`, so to suggest `-> Option<()>`
|
||||||
|
// we need to find a more accurate span to end up with
|
||||||
|
// fn g<'a>(mut x: impl Iterator<Item = &'_ ()>) -> Option<()>
|
||||||
|
sugg = vec![(span.with_hi(mut_ty.ty.span.lo()), String::new())];
|
||||||
|
owned_sugg = true;
|
||||||
|
}
|
||||||
if let Some(ty) = lt_finder.found {
|
if let Some(ty) = lt_finder.found {
|
||||||
if let TyKind::Path(None, Path { segments, .. }) = &ty.kind
|
if let TyKind::Path(None, Path { segments, .. }) = &ty.kind
|
||||||
&& segments.len() == 1
|
&& segments.len() == 1
|
||||||
|
@ -3101,8 +3113,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
||||||
sugg = vec![
|
sugg = vec![
|
||||||
(lt.span.with_hi(ty.span.hi()), "String".to_string()),
|
(lt.span.with_hi(ty.span.hi()), "String".to_string()),
|
||||||
];
|
];
|
||||||
}
|
} else if let TyKind::Slice(inner_ty) = &ty.kind {
|
||||||
if let TyKind::Slice(inner_ty) = &ty.kind {
|
|
||||||
// Don't suggest `-> [T]`, suggest `-> Vec<T>`.
|
// Don't suggest `-> [T]`, suggest `-> Vec<T>`.
|
||||||
sugg = vec![
|
sugg = vec![
|
||||||
(lt.span.with_hi(inner_ty.span.lo()), "Vec<".to_string()),
|
(lt.span.with_hi(inner_ty.span.lo()), "Vec<".to_string()),
|
||||||
|
@ -3110,8 +3121,8 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
if lt.kind == MissingLifetimeKind::Ampersand {
|
if owned_sugg {
|
||||||
err.multipart_suggestion_verbose(
|
err.multipart_suggestion_verbose(
|
||||||
format!("{pre} to return an owned value"),
|
format!("{pre} to return an owned value"),
|
||||||
sugg,
|
sugg,
|
||||||
|
|
|
@ -55,6 +55,11 @@ help: consider introducing a named lifetime parameter
|
||||||
|
|
|
|
||||||
LL | fn g<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { x.next() }
|
LL | fn g<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { x.next() }
|
||||||
| ++++ ~~~ ~~~
|
| ++++ ~~~ ~~~
|
||||||
|
help: alternatively, you might want to return an owned value
|
||||||
|
|
|
||||||
|
LL - fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() }
|
||||||
|
LL + fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<()> { x.next() }
|
||||||
|
|
|
||||||
|
|
||||||
error[E0106]: missing lifetime specifier
|
error[E0106]: missing lifetime specifier
|
||||||
--> $DIR/impl-trait-missing-lifetime-gated.rs:37:64
|
--> $DIR/impl-trait-missing-lifetime-gated.rs:37:64
|
||||||
|
@ -71,6 +76,11 @@ help: consider introducing a named lifetime parameter
|
||||||
|
|
|
|
||||||
LL | async fn i<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { x.next() }
|
LL | async fn i<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { x.next() }
|
||||||
| ++++ ~~~ ~~~
|
| ++++ ~~~ ~~~
|
||||||
|
help: alternatively, you might want to return an owned value
|
||||||
|
|
|
||||||
|
LL - async fn i(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() }
|
||||||
|
LL + async fn i(mut x: impl Iterator<Item = &'_ ()>) -> Option<()> { x.next() }
|
||||||
|
|
|
||||||
|
|
||||||
error[E0106]: missing lifetime specifier
|
error[E0106]: missing lifetime specifier
|
||||||
--> $DIR/impl-trait-missing-lifetime-gated.rs:47:37
|
--> $DIR/impl-trait-missing-lifetime-gated.rs:47:37
|
||||||
|
|
|
@ -13,6 +13,11 @@ help: consider introducing a named lifetime parameter
|
||||||
|
|
|
|
||||||
LL | fn g<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { x.next() }
|
LL | fn g<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { x.next() }
|
||||||
| ++++ ~~~ ~~~
|
| ++++ ~~~ ~~~
|
||||||
|
help: alternatively, you might want to return an owned value
|
||||||
|
|
|
||||||
|
LL - fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() }
|
||||||
|
LL + fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<()> { x.next() }
|
||||||
|
|
|
||||||
|
|
||||||
error[E0106]: missing lifetime specifier
|
error[E0106]: missing lifetime specifier
|
||||||
--> $DIR/impl-trait-missing-lifetime.rs:16:60
|
--> $DIR/impl-trait-missing-lifetime.rs:16:60
|
||||||
|
@ -29,6 +34,11 @@ help: consider introducing a named lifetime parameter
|
||||||
|
|
|
|
||||||
LL | async fn i<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { x.next() }
|
LL | async fn i<'a>(mut x: impl Iterator<Item = &'a ()>) -> Option<&'a ()> { x.next() }
|
||||||
| ++++ ~~~ ~~~
|
| ++++ ~~~ ~~~
|
||||||
|
help: alternatively, you might want to return an owned value
|
||||||
|
|
|
||||||
|
LL - async fn i(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() }
|
||||||
|
LL + async fn i(mut x: impl Iterator<Item = &'_ ()>) -> Option<()> { x.next() }
|
||||||
|
|
|
||||||
|
|
||||||
error: lifetime may not live long enough
|
error: lifetime may not live long enough
|
||||||
--> $DIR/impl-trait-missing-lifetime.rs:16:69
|
--> $DIR/impl-trait-missing-lifetime.rs:16:69
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue