rust-lang / rust-lang/rust-clippy
Suboptimal suggestion from implied_bounds_in_impls when adding bounds to associated type
Nobody has claimed this yet.
- 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
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
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