rust-lang / rust-lang/rust-clippy
Suggest use of associated type bounds where possible
Open
@y21 is already working on this.
Since Nov 17, 2024.
A-lint
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Now that associated type bounds are stabilized, I'd love to have a clippy lint (ideally with machine-applicable fix) that catches cases that could be written using associated type bounds.
This could start with the simplest cases:
- if a set of bounds contains a trait with an associated type, and has the form
T: TraitWithAssoc, <T as TraitWithAssoc>::Assoc: Bound, lint with a suggestion to merge intoT: TraitWithAssoc<Assoc: Bound>. - if a set of bounds contains a trait with an associated type, and has the form
T: TraitWithassoc<Assoc = U>, U: Bound, andUisn't used for anything else, lint with a suggestion to merge intoT: TraitWithAssoc<Assoc: Bound>and delete the declaration ofU.
Advantage
This simplifies bounds and makes them easier to follow, with less indirection. In some cases it also eliminates the need for the complex <T as TraitWithAssoc>::Assoc syntax.
Drawbacks
No response
Example
fn func<T>(iter: T)
where
T: Iterator,
<T as Iterator>::Item: Display,
Could be written as:
fn func<T>(iter: T)
where
T: Iterator<Item: Display>,
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.
Assessment
This issue has not been assessed yet.