rust-lang / rust-lang/rust-clippy

New lint: Warn against lifetimes being the same, e.g. `'a: 'b, 'b: 'a`

Open
#7,940 2 comments 2 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

Lint against lifetime arguments that come with bounds making them equal.

For example

fn foo<'a: 'b, 'b: 'a>(x: &'a (), y: &'b ()) {}

could be written

fn foo<'a>(x: &'a (), y: &'a ()) {}

Most notably this lint must also be able to take implied bound into consideration, e.g.

fn foo<'a: 'b, 'b>(x: &'a &'b ()) {}

For some background: I came across this question on URLO; I'm unhappy that neither the Rust compiler nor clippy would be pointing out the problem I pointed out in my answer, even though it's obvious that two lifetimes in question are only accidentally restricted to be equal.

Categories
  • Kind: Not quite sure, possibly suspicions, or at least complexity.

What is the advantage of the recommended code over the original code

Shorter, more clear; catches some cases where you're having unnecessary bounds or misdesigned an API

Drawbacks

Probably none?

I'm not sure under what conditions the lint should fire in trait impls. I suppose there would be cases where bounds from a trait impl and a trait method can end up restricting two lifetimes to be the same without there being a way to avoid it? (I don't have an example (yet) though...)

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 source files, tests, or entry points are named. Start by examining the lifetime-bound examples and the linked URLO discussion, then resolve how implied bounds and trait impls should affect the lint. Done means the lint behavior and its applicability are defined for the stated cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
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.