-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add hidden_static_lifetime
lint
#10123
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
Conversation
r? @Jarcho (rustbot has picked a reviewer for you, use r? to override) |
b646263
to
bf6fd45
Compare
This needs to be limited by quite a bit. The only cases which this is valid for are when the lifetime is used only in immutable references which are neither behind a mutable reference, nor used in function types; and mutable references to static types. Some valid examples:
Some invalid examples:
Whether switching struct lifetime parameters to |
How could I test this? |
The current code doesn't do anything to avoid linting on panic. Worst case you could do |
The lint doesn't, but it seems that a code that should trigger the lint, doesn't, if the code has a Also, fn l<'l, T>() -> &'l mut T where T: 'static + Copy {
unsafe { *std::ptr::null() }
} I'm clueless, I don't know how to test this lint. |
Try |
Thanks for the snippet, it works. |
529fb18
to
b523269
Compare
b523269
to
90280d8
Compare
This needs a test for with something like |
Struct fields follow the same rules that other types do. Some more examples: struct S<T>(T);
struct Ref<'a, T: 'a>(&'a T);
struct Both<'a, 'b, T> {
owned: T,
borrow: &'a T,
mut_borrow: &'b mut T,
}
fn f1<'a, 'b>() -> S<&'a mut Ref<'b, u32>> {} // can't change
fn f2<'a, 'b>() -> &'a Both<'a, 'b, &'a str> {} // can't change
fn f3<'a, 'b, 'c>() -> &'a Both<'a, 'b, &'c str> {} // 'a can be 'static
fn f4<'a, 'b>() -> &'a Both<'a, 'b, &'static str> {} // 'a and 'b can be 'static
fn f5<'a> -> Ref<'a, fn(S<Ref<'a, u32>>)> {} // can't change Handling structs correctly is going to be a fair bit of work and is better handled not at the HIR level since substitutions need to be taken into account. I would say it's better to just handle the simple case of a top level reference (or multiple references). Unless you really want to go all the way with it. A couple of notes about the the implementation.
|
What other levels can I access? Isn't Clippy specifically for checking the HIR? |
Also, making this lint only trigger for function returns and ignore structs is probably the best idea. A new lint can possibly be implemented in the future for struct checking. But it doesn't make sense to check structs with a lint in the |
026ed38
to
f46ebca
Compare
26f15f8
to
d1b1f3e
Compare
This should use a |
}; | ||
}; | ||
} else { | ||
span_lint_and_help(cx, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the same as a 'static
bound.
if region_predicate.lifetime.hir_id.owner == generic.hir_id.owner { | ||
lifetime_is_used = true; | ||
} else { | ||
span_lint_and_help(cx, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the same as a 'static
bound.
} | ||
|
||
// true = lifetime isn't used (success) | ||
fn lifetime_not_used_in_ty(ret_ty: &Ty<'_>, generic: &GenericParam<'_>) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be merged with the previous function. While the type is a reference you're unwrapping it looking for lifetimes to change to 'static
. Once you hit a type which isn't a reference you're then looking to see if the lifetime is used again.
You would use the middle layer's representation of everything. It can all be retrieved from various |
I've been trying to convert the old code to a new lint using The last commit contains my current code for converting this lint to a Visitor, it doesn't work and I don't even know if I'm doing things right. Is it really necessary? If so, is there something I'm doing really wrong that I should be fixing right now? |
You misunderstood how to use a visitor there. You should only need to override Essentially something like (not valid code):
|
☔ The latest upstream changes (presumably #10313) made this pull request unmergeable. Please resolve the merge conflicts. |
It's been 48 - 49 days since this PR was created. I'm very sorry for wasting your time. Once again I chose an issue with a scope far bigger than I imagined, and thought it would be easy. That's a childish mentality and, in all future PRs I should stick to easier, quicker issues. For anyone attempting this in the future: this last commit contains my work-in-progress code (It obviously doesn't work). You may also want to check the last commit before the revamp for some support functions or notes. Im very sorry to my reviewer, @Jarcho for wasting their time. I'm going to close this PR and unnassign myself from the issue. If anyone wants to make another PR about #10108, it's completely free. |
Fixes #10108
changelog: New lint: [
hidden_static_lifetime
]:#10123