rust-lang / rust-lang/rust-clippy
Suggest using Self::AssociatedType for trait implementations
Open
Nobody has claimed this yet.
A-lint
E-medium
L-suggestion
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Suggests replacing a concrete type like State with the associated type of the trait.
Categories (optional)
- Kind:
clippy::styleorclippy::pedantic
What is the advantage of the recommended code over the original code
- the name of the associated type can be changed, without having to change the code that uses the associated type
Drawbacks
Might make the code more difficult to read.
Example
pub struct Example;
pub enum State {
A,
B,
C,
}
pub trait SimpleTrait {
type Associated;
fn do_something_with_associated(&self, associated: &Self::Associated);
}
impl SimpleTrait for Example {
type Associated = State;
fn do_something_with_associated(&self, associated: &Self::Associated) {
match associated {
State::A => println!("State::A"),
State::B => println!("State::B"),
State::C => println!("State::C"),
}
}
}
Could be written as:
impl SimpleTrait for Example {
type Associated = State;
fn do_something_with_associated(&self, associated: &Self::Associated) {
match associated {
Self::Associated::A => println!("State::A"),
Self::Associated::B => println!("State::B"),
Self::Associated::C => println!("State::C"),
}
}
}
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 with the Rust playground example in the issue and verify whether the proposed Self::Associated syntax is valid and appropriate for a Clippy lint. Done means a decided lint design that detects concrete associated-type references in trait implementations, with its category and behavior documented and covered by appropriate Clippy tests.
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