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

Do not propose to elide lifetimes if this causes an ambiguity #13929

Merged
merged 1 commit into from
Jan 10, 2025

Conversation

samueltardieu
Copy link
Contributor

@samueltardieu samueltardieu commented Jan 2, 2025

Some lifetimes in function return types are not bound to concrete content and can be set arbitrarily. Clippy should not propose to replace them by the default '_ lifetime if such a lifetime cannot be determined unambigously.

I added a field to the LifetimeChecker and Usage to flag lifetimes that cannot be replaced by default ones, but it feels a bit hacky.

Fix #13923

changelog: [needless_lifetimes]: remove false positives by checking that lifetimes can indeed be elided

@rustbot
Copy link
Collaborator

rustbot commented Jan 2, 2025

r? @xFrednet

rustbot has assigned @xFrednet.
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 Jan 2, 2025
Copy link
Member

@xFrednet xFrednet 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. Comments which are hopefully simple enough to fix.

clippy_lints/src/lifetimes.rs Outdated Show resolved Hide resolved
clippy_lints/src/lifetimes.rs Show resolved Hide resolved
clippy_lints/src/lifetimes.rs Outdated Show resolved Hide resolved
@samueltardieu samueltardieu force-pushed the push-zsoyywztwsty branch 2 times, most recently from 488a230 to e2ff1fb Compare January 7, 2025 19:22
@samueltardieu samueltardieu requested a review from xFrednet January 7, 2025 20:41
type Result = ControlFlow<bool>;

fn visit_lifetime(&mut self, lifetime: &Lifetime) -> Self::Result {
ControlFlow::Break(lifetime.is_elided() || lifetime.is_anonymous())
Copy link
Member

Choose a reason for hiding this comment

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

This currently breaks on the first lifetime it finds. Meaning that (x: Cx<'_, 'tcx>) would say it's anonymous, right?

It feels like this should only break if it finds a user-defined lifetime 🤔

Could you also add a test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but:

  • when checking the output type, we don't care about the lifetime being anonymous (we just want to know if we have at least one lifetime)
  • we check the input types only when the function supports lifetime elision, which is not the case here because we have at least two input lifetimes ('_ and 'tcx)

Does that make sense? I've added a test, let me know if this is what you had in mind.

Copy link
Member

Choose a reason for hiding this comment

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

It make sense, I didn't know that two LTs implied that they can't be elapsed

clippy_lints/src/lifetimes.rs Show resolved Hide resolved
tests/ui/needless_lifetimes.rs Outdated Show resolved Hide resolved
@samueltardieu samueltardieu force-pushed the push-zsoyywztwsty branch 2 times, most recently from 1455ca0 to cadbe44 Compare January 8, 2025 11:48
Some lifetimes in function return types are not bound to concrete
content and can be set arbitrarily. Clippy should not propose to replace
them by the default `'_` lifetime if such a lifetime cannot be
determined unambigously.
Copy link
Member

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

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

LGTM, thank you the update!

Roses are red,
Violets are blue,
This lint doesn't elide,
needed lifetimes anymore.

@xFrednet xFrednet added this pull request to the merge queue Jan 10, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 10, 2025
@samueltardieu
Copy link
Contributor Author

Silly me.

@xFrednet changelog added.

@xFrednet xFrednet added this pull request to the merge queue Jan 10, 2025
Merged via the queue into rust-lang:master with commit 5c2601a Jan 10, 2025
9 checks passed
@samueltardieu samueltardieu deleted the push-zsoyywztwsty branch January 10, 2025 12:15
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.

False positive from clippy::needless_lifetimes
3 participants