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
[use_self
]: Make it aware of lifetimes
#12386
Conversation
clippy_lints/src/use_self.rs
Outdated
// Ensure the type we encounter and the one from the impl have the same lifetime parameters. It may be that | ||
// the lifetime parameters of `ty` are ellided (`impl<'a> Foo<'a> { fn new() -> Self { Foo{..} } }`, in | ||
// which case we must still trigger the lint. | ||
&& (same_lifetimes(ty, impl_ty) || has_no_lifetime(ty)) |
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.
has_no_lifetime
makes less operations so I think it'd be better to have it run first.
&& (same_lifetimes(ty, impl_ty) || has_no_lifetime(ty)) | |
&& (has_no_lifetime(ty) || same_lifetimes(ty, impl_ty)) |
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.
My point of view was that I kind of rarely use Foo
without a lifetime in an impl<'a> Foo<'a>
block. I feel it is more common to have methods taking a Foo<'b>
as parameter, so I optimized for this scenario.
I don't have a strong opinion on the matter, though. I do not mind changing it.
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.
Me neither so let's wait for a clippy reviewer to see waht they prefer. :)
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.
Sorry for the delay. In general, it's better to optimistically check special cases before the complex general case.
So checking for no lifetimes would be better.
.iter() | ||
.zip(args_b.iter()) | ||
.all(|(arg_a, arg_b)| match (arg_a.unpack(), arg_b.unpack()) { | ||
(GenericArgKind::Lifetime(inner_a), GenericArgKind::Lifetime(inner_b)) => inner_a == inner_b, |
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.
I don't think the case where a lifetime is infer
is handled here, right?
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.
At this point the lifetime will have been "computed" already so even if '_
is used, it won't appear here. Sorry for the noise!
The 2 comments have been addressed. I've also rebased on top of |
Have the lint trigger even if `Self` has generic lifetime parameters. ```rs impl<'a> Foo<'a> { type Item = Foo<'a>; // Can be replaced with Self fn new() -> Self { Foo { // No lifetime, but they are inferred to be that of Self // Can be replaced as well ... } } // Don't replace `Foo<'b>`, the lifetime is different! fn eq<'b>(self, other: Foo<'b>) -> bool { .. } ``` Fixes rust-lang#12381
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.
Sorry for the delay, I've been busy these days and this PR got in between the cracks =^w^=
Just this little question, you can either change it to false
, or come up with a more complex pattern matching to still lint those cases and achieve enlightenment ✨
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.
LGTM, thanks! ❤️
@GuillaumeGomez, do you also approve this? ^w^
I do! Thanks for your review. ;) |
@bors r+ |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
``` error: unnecessary structure name repetition --> aya/src/bpf.rs:198:57 | 198 | pub fn btf(&mut self, btf: Option<&'a Btf>) -> &mut EbpfLoader<'a> { | ^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_self note: the lint level is defined here --> aya/src/lib.rs:42:5 | 42 | clippy::use_self, | ^^^^^^^^^^^^^^^^ error: unnecessary structure name repetition --> aya/src/bpf.rs:222:54 | 222 | pub fn allow_unsupported_maps(&mut self) -> &mut EbpfLoader<'a> { | ^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_self error: unnecessary structure name repetition --> aya/src/bpf.rs:243:69 | 243 | pub fn map_pin_path<P: AsRef<Path>>(&mut self, path: P) -> &mut EbpfLoader<'a> { | ^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_self error: unnecessary structure name repetition --> aya/src/bpf.rs:292:15 | 292 | ) -> &mut EbpfLoader<'a> { | ^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_self error: unnecessary structure name repetition --> aya/src/bpf.rs:313:73 | 313 | pub fn set_max_entries(&mut self, name: &'a str, size: u32) -> &mut EbpfLoader<'a> { | ^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_self error: unnecessary structure name repetition --> aya/src/bpf.rs:335:56 | 335 | pub fn extension(&mut self, name: &'a str) -> &mut EbpfLoader<'a> { | ^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_self error: unnecessary structure name repetition --> aya/src/bpf.rs:353:75 | 353 | pub fn verifier_log_level(&mut self, level: VerifierLogLevel) -> &mut EbpfLoader<'a> { | ^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_self ``` See rust-lang/rust-clippy#12386.
``` error: unnecessary structure name repetition --> aya/src/bpf.rs:198:57 | 198 | pub fn btf(&mut self, btf: Option<&'a Btf>) -> &mut EbpfLoader<'a> { | ^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_self note: the lint level is defined here --> aya/src/lib.rs:42:5 | 42 | clippy::use_self, | ^^^^^^^^^^^^^^^^ error: unnecessary structure name repetition --> aya/src/bpf.rs:222:54 | 222 | pub fn allow_unsupported_maps(&mut self) -> &mut EbpfLoader<'a> { | ^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_self error: unnecessary structure name repetition --> aya/src/bpf.rs:243:69 | 243 | pub fn map_pin_path<P: AsRef<Path>>(&mut self, path: P) -> &mut EbpfLoader<'a> { | ^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_self error: unnecessary structure name repetition --> aya/src/bpf.rs:292:15 | 292 | ) -> &mut EbpfLoader<'a> { | ^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_self error: unnecessary structure name repetition --> aya/src/bpf.rs:313:73 | 313 | pub fn set_max_entries(&mut self, name: &'a str, size: u32) -> &mut EbpfLoader<'a> { | ^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_self error: unnecessary structure name repetition --> aya/src/bpf.rs:335:56 | 335 | pub fn extension(&mut self, name: &'a str) -> &mut EbpfLoader<'a> { | ^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_self error: unnecessary structure name repetition --> aya/src/bpf.rs:353:75 | 353 | pub fn verifier_log_level(&mut self, level: VerifierLogLevel) -> &mut EbpfLoader<'a> { | ^^^^^^^^^^^^^^ help: use the applicable keyword: `Self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#use_self ``` See rust-lang/rust-clippy#12386.
Have the lint trigger even if
Self
has generic lifetime parameters.Fixes #12381
Please write a short comment explaining your change (or "none" for internal only changes)
changelog: [
use_self
]: Have the lint trigger even ifSelf
has generic lifetime parameters