rust-lang / rust-lang/rust-clippy

Detect use of unnecessary `dyn` in function parameter

Open
#13,685 2 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

Scan for all uses of a function, and check if the &dyn T can be replaced by &impl T.

Advantage
  • Avoid the overhead introduced by dynamic dispatch
Drawbacks

Code bloat and compile overhead.

Example
// A private trait that takes a dyn reference.
trait Trait {
  fn f(writer: &mut dyn std::io::Write);
}

// Using that trait with concrete types.
fn use_trait(obj: impl Trait) {
  obj.f(&mut Vec::new());
}

Could be written as:

trait Trait {
  fn f(writer: &mut impl std::io::Write);
}

fn use_trait(obj: impl Trait) {
  obj.f(&mut Vec::new());
}

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

No implementation file or test is named. Start by reviewing how rust-clippy registers lints and how existing lints inspect function parameters and their uses. Done means detecting eligible &dyn T parameters that can become &impl T, while accounting for the stated code-bloat and compile-overhead drawbacks.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.