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

Extend needless_lifetimes to suggest eliding impl lifetimes #13286

Merged
merged 9 commits into from
Oct 1, 2024

Conversation

smoelius
Copy link
Contributor

Example:

error: the following explicit lifetimes could be elided: 'a
  --> tests/ui/needless_lifetimes.rs:332:10
   |
LL |     impl<'a> Foo for Baz<'a> {}
   |          ^^              ^^
   |
help: elide the lifetimes
   |
LL -     impl<'a> Foo for Baz<'a> {}
LL +     impl Foo for Baz<'_> {}

The main change is in how impl lifetime uses are tracked. Previously, a hashmap was created, and lifetimes were removed from the hashmap as their uses were discovered. However, the uses are needed to generate elision suggestions. So, now, uses are added to the hashmap as they are discovered.

The PR is currently organized as six commits, which I think are self-explanatory:

  • Extend needless_lifetimes to suggest eliding impl lifetimes
  • Reorder functions [not strictly necessary, but IMHO, the code is better structured as a result]
  • Fix lifetime tests
  • Fix non-lifetime tests
  • Fix clippy_lints and clippy_utils
  • Fix typo in needless_lifetimes test

r? @Alexendoo (I think you are needless_lifetimes' primary author? Sorry if I have this wrong.)


changelog: Extend needless_lifetimes to suggest eliding impl lifetimes

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 18, 2024
@bors
Copy link
Contributor

bors commented Sep 12, 2024

☔ The latest upstream changes (presumably #13390) made this pull request unmergeable. Please resolve the merge conflicts.

@smoelius smoelius force-pushed the elidable-impl-lifetimes branch 2 times, most recently from 07b5310 to c64794a Compare September 12, 2024 17:05
@bors
Copy link
Contributor

bors commented Sep 22, 2024

☔ The latest upstream changes (presumably #13440) made this pull request unmergeable. Please resolve the merge conflicts.

@smoelius
Copy link
Contributor Author

Hey, @Alexendoo. Short of a full review, could I ask what your thoughts are on this PR?

Also, I would understand if your plate is too full. Would you like be to ask rustbot to select another reviewer?

@bors
Copy link
Contributor

bors commented Sep 25, 2024

☔ The latest upstream changes (presumably #13442) made this pull request unmergeable. Please resolve the merge conflicts.

@Alexendoo
Copy link
Member

Sorry yeah been busy, I think I've reviewed some stuff for this lint but it's been a while and I've forgotten most of it 😅

The change seems like a natural extension but it does make me wonder if we should split the lint into cases that need '_ and ones that don't, but that can be separate since we already suggest '_ sometimes

Some of the added extra_unused_lifetimes cases in https://github.com/rust-lang/rust-clippy/actions/runs/10985315434#summary-30497090664 look incorrect though e.g. https://docs.rs/rayon/1.10.0/src/rayon/iter/copied.rs.html#28

@smoelius
Copy link
Contributor Author

Sorry yeah been busy, I think I've reviewed some stuff for this lint but it's been a while and I've forgotten most of it 😅

NP. Thanks very much for your comments.

The change seems like a natural extension but it does make me wonder if we should split the lint into cases that need '_ and ones that don't, but that can be separate since we already suggest '_ sometimes

👍

Some of the added extra_unused_lifetimes cases in https://github.com/rust-lang/rust-clippy/actions/runs/10985315434#summary-30497090664 look incorrect though e.g. https://docs.rs/rayon/1.10.0/src/rayon/iter/copied.rs.html#28

I was using GenericArg when I should have been using GenericArgs. The problem is fixed, and I added the case you pointed out as a test case.

Comment on lines 652 to 569
let mut checker = LifetimeChecker::<hir_nested_filter::None>::new(cx, hs);
let mut checker = LifetimeChecker::<hir_nested_filter::None>::new(cx, def_ids);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could collect into the HashMap LifetimeChecker wants directly rather than creating a temporary Vec

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe pass the Generics into LifetimeChecker::new and have the collect done in there? (Maybe this is what you were suggesting.)

Copy link
Member

@Alexendoo Alexendoo Sep 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just thinking of passing in FxHashMap<LocalDefId, Vec<Usage>> but yeah yours is better since there's two places the collect is being done already

@Alexendoo
Copy link
Member

Thanks!

@bors r+

@bors
Copy link
Contributor

bors commented Oct 1, 2024

📌 Commit 54e4761 has been approved by Alexendoo

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Oct 1, 2024

⌛ Testing commit 54e4761 with merge db1bda3...

@bors
Copy link
Contributor

bors commented Oct 1, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: Alexendoo
Pushing db1bda3 to master...

@bors bors merged commit db1bda3 into rust-lang:master Oct 1, 2024
8 checks passed
@smoelius
Copy link
Contributor Author

smoelius commented Oct 1, 2024

Thank you!

@smoelius smoelius deleted the elidable-impl-lifetimes branch October 1, 2024 00:52
dburgener added a commit to dburgener/lalrpop that referenced this pull request Oct 4, 2024
Tracking lifetime support in impls was added in recent nightly clippy.

rust-lang/rust-clippy#13286
github-merge-queue bot pushed a commit to lalrpop/lalrpop that referenced this pull request Oct 4, 2024
Tracking lifetime support in impls was added in recent nightly clippy.

rust-lang/rust-clippy#13286
kpreid added a commit to kpreid/all-is-cubes that referenced this pull request Oct 4, 2024
I'm not entirely sure I like the non-uniformity of giving a name to the
lifetime in the type only if it is used in the body, but I do appreciate
reducing the generic parameter count in many impl blocks, and all of the
actual cases seem fine.

This additional lint came from <rust-lang/rust-clippy#13286>
“Extend needless_lifetimes to suggest eliding impl lifetimes”.
ojeda added a commit to ojeda/linux that referenced this pull request Oct 12, 2024
In nightly Clippy (i.e. Rust 1.83.0), the `needless_lifetimes` lint has
been extended [1] to suggest eliding `impl` lifetimes, e.g.

    error: the following explicit lifetimes could be elided: 'a
    --> rust/kernel/list.rs:647:6
        |
    647 | impl<'a, T: ?Sized + ListItem<ID>, const ID: u64> FusedIterator for Iter<'a, T, ID> {}
        |      ^^                                                                  ^^
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes
        = note: `-D clippy::needless-lifetimes` implied by `-D warnings`
        = help: to override `-D warnings` add `#[allow(clippy::needless_lifetimes)]`
    help: elide the lifetimes
        |
    647 - impl<'a, T: ?Sized + ListItem<ID>, const ID: u64> FusedIterator for Iter<'a, T, ID> {}
    647 + impl<T: ?Sized + ListItem<ID>, const ID: u64> FusedIterator for Iter<'_, T, ID> {}

Thus clean them.

Link: rust-lang/rust-clippy#13286 [1]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
intel-lab-lkp pushed a commit to intel-lab-lkp/linux that referenced this pull request Oct 12, 2024
In nightly Clippy (i.e. Rust 1.83.0), the `needless_lifetimes` lint has
been extended [1] to suggest eliding `impl` lifetimes, e.g.

    error: the following explicit lifetimes could be elided: 'a
    --> rust/kernel/list.rs:647:6
        |
    647 | impl<'a, T: ?Sized + ListItem<ID>, const ID: u64> FusedIterator for Iter<'a, T, ID> {}
        |      ^^                                                                  ^^
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes
        = note: `-D clippy::needless-lifetimes` implied by `-D warnings`
        = help: to override `-D warnings` add `#[allow(clippy::needless_lifetimes)]`
    help: elide the lifetimes
        |
    647 - impl<'a, T: ?Sized + ListItem<ID>, const ID: u64> FusedIterator for Iter<'a, T, ID> {}
    647 + impl<T: ?Sized + ListItem<ID>, const ID: u64> FusedIterator for Iter<'_, T, ID> {}

Thus clean them.

Link: rust-lang/rust-clippy#13286 [1]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
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.

4 participants