rust-lang / rust-lang/rust-clippy

Suggest to replace type parameter with impl

Open
#11,297 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What it does

fn f<A: Sized>(x: A) can be replaced by fn(x: impl Sized), which is more readable and 99.9% sufficient for the purpose of specifying a parameter implementing a trait.

Advantage

More readable code (type is just after the variabile, no eyeballing between type parameters and argument types).

It is useful to have it as a lint because sometimes changes in the code make it possible to move a type parameter to impl, but the programmer might miss the opportunity.

Drawbacks
  • Personal preference (some people might not like impl). In the case of return types impl has consequences (not being able to choose the return type) but in the case of function arguments this is not an issue.
  • If the function has been turbofished, some editing is necessary as the parameter must be cast to the desired type instead.
Example
<code>
fn f<A: Sized>(a: A){
    
}

Could be written as:

<code>
fn f(a: impl Sized){
    
}

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

The issue proposes a Rust Clippy lint that suggests replacing a generic type parameter with an argument-position impl trait. No files, tests, or entry points are named; first locate the relevant Clippy lint infrastructure and related lint tests. Done means the lint handles the stated function-argument pattern and accounts for the turbofish and return-type drawbacks described.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.