rust-lang / rust-lang/rust-clippy
Unifying an invariant lifetime with a covariant one is almost always bad
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Triggers on code of the form &'a mut Something<'a>. (The mut is important here.) I see people write this when they're not sure what to put in each lifetime position, but it's (as far as I can tell!) basically always wrong, and causes weird lifetime errors in other places by unifying the two lifetimes. This is particularly annoying when it appears as a function argument, as it causes weird errors in the caller.
Unless I've missed something this code should almost always be &'b mut Something<'a> instead.
You'd probably also want this to point out cases of &'a mut &'a Something.
Advantage
Avoids unifying the invariant lifetime of the mut reference with the (often covariant) lifetime of the (often not mut) reference inside the parameterized type.
More importantly, avoids distant confusing errors from rustc attempting to solve lifetime constraints in callers.
Drawbacks
It's possible that this pattern has a use and I've never seen it -- if it does, this would false positive on such code.
Example
&'a mut Something<'a>
Could be written as:
&'b mut Something<'a>
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No files, tests, or entry points are named. Start by reviewing how Clippy implements lifetime-related lints, then assess the examples involving &'a mut Something<'a> and &'a mut &'a Something. Done means establishing the supported patterns and false-positive boundaries before adding the lint and its validation coverage.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100