rust-lang / rust-lang/rust-clippy

Unifying an invariant lifetime with a covariant one is almost always bad

Open
#12,657 1 comment 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.