Skip to content
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

Merged
merged 1 commit into from Mar 14, 2024
Merged

Conversation

Ethiraric
Copy link
Contributor

Have the lint trigger even if Self has generic lifetime parameters.

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 #12381

Please write a short comment explaining your change (or "none" for internal only changes)

changelog: [use_self]: Have the lint trigger even if Self has generic lifetime parameters

@rustbot
Copy link
Collaborator

rustbot commented Feb 29, 2024

r? @blyxyas

rustbot has assigned @blyxyas.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Feb 29, 2024
// 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))
Copy link
Member

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.

Suggested change
&& (same_lifetimes(ty, impl_ty) || has_no_lifetime(ty))
&& (has_no_lifetime(ty) || same_lifetimes(ty, impl_ty))

Copy link
Contributor Author

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.

Copy link
Member

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. :)

Copy link
Member

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,
Copy link
Member

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?

Copy link
Member

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!

@Ethiraric
Copy link
Contributor Author

The 2 comments have been addressed. I've also rebased on top of master.

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
Copy link
Member

@blyxyas blyxyas left a 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

clippy_lints/src/use_self.rs Show resolved Hide resolved
Copy link
Member

@blyxyas blyxyas left a 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^

@GuillaumeGomez
Copy link
Member

I do! Thanks for your review. ;)

@blyxyas
Copy link
Member

blyxyas commented Mar 14, 2024

@bors r+

@bors
Copy link
Collaborator

bors commented Mar 14, 2024

📌 Commit f3879b3 has been approved by blyxyas

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Mar 14, 2024

⌛ Testing commit f3879b3 with merge 8a78128...

@bors
Copy link
Collaborator

bors commented Mar 14, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: blyxyas
Pushing 8a78128 to master...

@bors bors merged commit 8a78128 into rust-lang:master Mar 14, 2024
5 checks passed
tamird added a commit to tamird/aya that referenced this pull request Mar 24, 2024
```
  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.
@tamird tamird mentioned this pull request Mar 24, 2024
tamird added a commit to aya-rs/aya that referenced this pull request Mar 24, 2024
```
  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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

use_self does not notice Self with lifetimes
5 participants