rust-lang / rust-lang/rust-clippy

Suboptimal suggestion from implied_bounds_in_impls when adding bounds to associated type

Open
#13,617 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

When returning a type with redundant bounds on an associated type, implied_bounds_in_impls's suggestion duplicates the implied bound

Reproducer

I tried this code (playground]:

<pub trait MyTrait {
    type MyAssociatedType;
}

pub trait MyStricterTrait: MyTrait<MyAssociatedType: Clone> {}

pub struct MyStruct;
pub struct MyStruct2;

impl MyTrait for MyStruct {
    type MyAssociatedType = u64;
}

impl MyTrait for MyStruct2 {
    type MyAssociatedType = Vec<u64>;
}

impl MyStricterTrait for MyStruct {
}

pub fn f() -> impl MyStricterTrait + MyTrait<MyAssociatedType: Copy> {
    MyStruct
}

I expected to see this happen:

warning: this bound is already specified as the supertrait of `MyStricterTrait`
  --> src/lib.rs:21:38
   |
21 | pub fn f() -> impl MyStricterTrait + MyTrait<MyAssociatedType: Copy> {
   |                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#implied_bounds_in_impls
   = note: `#[warn(clippy::implied_bounds_in_impls)]` on by default
help: try removing this bound
   |
21 - pub fn f() -> impl MyStricterTrait + MyTrait<MyAssociatedType: Copy> {
21 + pub fn f() -> impl MyStricterTrait<MyAssociatedType: Copy> {
   |

Instead, this happened:

warning: this bound is already specified as the supertrait of `MyStricterTrait`
  --> src/lib.rs:21:38
   |
21 | pub fn f() -> impl MyStricterTrait + MyTrait<MyAssociatedType: Copy> {
   |                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#implied_bounds_in_impls
   = note: `#[warn(clippy::implied_bounds_in_impls)]` on by default
help: try removing this bound
   |
21 - pub fn f() -> impl MyStricterTrait + MyTrait<MyAssociatedType: Copy> {
21 + pub fn f() -> impl MyStricterTrait<MyAssociatedType: Copy> + MyTrait<MyAssociatedType: Copy> {
   |
Version

default versions on the Rust playground (rustc 1.82.0)

Additional Labels

No response

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 linked Rust Playground reproducer with the implied_bounds_in_impls lint enabled and compare the actual suggestion with the expected one. Locate the lint implementation and its suggestion-building entry point, then verify the result against this associated-type bound case. Done means the diagnostic removes the redundant bound without duplicating it.

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
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.