rust-lang / rust-lang/rust-clippy

Suggest use of associated type bounds where possible

Open
#12,925 0 comments 1 reaction 1 assignee View on GitHub

@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 into T: 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, and U isn't used for anything else, lint with a suggestion to merge into T: TraitWithAssoc<Assoc: Bound> and delete the declaration of U.
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

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.