rust-lang / rust-lang/rust-clippy

`type_repetition_in_bounds` misses `for<'a>` when combining higher-rank trait bounds

Open
#9,336 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

Clippy omits the for<'a> part of a higher-rank trait bound when trying to combine two trait bounds.

Also happens on 1.63.0 stable.

Reproducer

I tried this code:

pub trait Foo<T> {}

pub struct Bar<S, T>(S, T)
where
    S: Foo<T>,
    for<'a> S: Foo<&'a T>;

fn main() {}

I expected to see this happen:

warning: this type has already been used as a bound predicate
 --> src/main.rs:6:5
  |
6 |     for<'a> S: Foo<&'a T>;
  |     ^^^^^^^^^^^^^^^^^^^^^
  |
  = note: requested on the command line with `-W clippy::type-repetition-in-bounds`
  = help: consider combining the bounds: `S: Foo<T> + for<'a> Foo<&'a T>`
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#type_repetition_in_bounds

or maybe,

  = help: consider combining the bounds: `for<'a> S: Foo<T> + Foo<&'a T>`

Instead, this happened:

  = help: consider combining the bounds: `S: Foo<T> + Foo<&'a T>`
Version
rustc 1.65.0-nightly (29e4a9ee0 2022-08-10)
binary: rustc
commit-hash: 29e4a9ee0253cd39e552a77f51f11f9a5f1c41e6
commit-date: 2022-08-10
host: x86_64-unknown-linux-gnu
release: 1.65.0-nightly
LLVM version: 14.0.6
Additional Labels

@rustbot label +I-suggestion-causes-error

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

Start by running the provided Rust reproducer with clippy::type-repetition-in-bounds enabled and inspect the lint's bound-combining logic. Done means the help suggestion preserves the higher-ranked for<'a> bound while combining the repeated predicates, with the displayed output matching one of the expected forms.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.