-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add lint to detect lifetime parameters that could be replaced by 'static
#120344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This could be implemented as an extension to the |
@compiler-errors Contravariance is a concern because the following lifetime fn foo<'a>() -> fn(&'a i32) -> i32 {
fn bar<'b>(x: &'b i32) -> i32 {
0
}
bar
} I don't think it's much of a problem though. It just amounts to checking if the lifetime is used contravariantly in the output. Invariance is never going to be a problem because we can't return an invariant type with non- The main motivation is to educate about static promotion (see #48998). Even without that I don't see how it would be any less useful than |
That's not true. In the rust compiler, for example, we have a
Also, this only works when the data pointed-to actually outlives |
When I wrote this up initially I had some text about
You're right. I completely blanked on the fact that even though the type has a non- fn foo<'a>() -> Result<&'a mut u8, u8> {
Err(0)
}
Something like this, right? fn foo<T>(_t: T) -> Option<&'static Option<T>> {None}
fn bar(a: &u8) {foo(a);} I'll be honest, I didn't realize implied bounds on lifetimes were a thing. This is more complex than I thought. I'll stop making uninformed (or informed for that matter) comments about the implementation details now. I'm still curious why you think that the usefulness here is any lower than with |
Code
Current output
Desired output
Rationale and extra context
In some cases lifetime parameters might as well be
'static
because of subtyping. Using a'static
lifetime can simplify the signature by avoiding generics and help teach beginners that the only reason this works is because of static promotion. Adding something about static promotion to the warning could also make sense. I'm not too happy with my "desired output".This is also related to the
unused_lifetimes
lint, but that doesn't catch this either and it's not exactly "unused".Credit goes to @estebank for the suggestion in #48998 (comment)
Other cases
No response
Rust Version
Anything else?
No response
The text was updated successfully, but these errors were encountered: