rust-lang / rust-lang/rust-clippy
Flag duplicate constraints
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
It would be nice to have a lint that flags duplicate constraints on generic parameters. For example,
struct Foo<T>(T);
impl<T> Foo<T>
where
T: Ord,
{
fn bar(&self)
where
T: Ord,
{
}
}
Here, T is constrained on both the impl and a method. There is nothing inherently wrong with it, but it's duplication and I can't think of a reason when it's desired. On the contrary, say the user wants to modify the constraint, now it may be easy to forget one location and end up with a big mess (which may or may not be detected early depending on whether constraints are loosened or not).
Lint Name
duplicate-constraints
Category
suspicious
Advantage
- The lint would raise awareness that the user in all likelihood wants to specify a certain constraint only on the
implOR a method - As alluded to above, having duplicate constraints may become a correctness issue in the presence of changes, if one location is forgotten to be adjusted
- It's also just less code and we get back to having a single source of truth
Drawbacks
None known.
Example
struct Foo<T>(T);
impl<T> Foo<T>
where
T: Ord,
{
fn bar(&self)
where
T: Ord,
{
}
}
Could be written as:
struct Foo<T>(T);
impl<T> Foo<T>{
fn bar(&self)
where
T: Ord,
{
}
}
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
No implementation file or test is named; start by locating existing Clippy lints in the suspicious category and their tests. Use the provided Rust example to define duplicate generic constraints across impl and method scopes, and consider the lint complete when it reports duplicate-constraints while leaving the nonduplicated form clean.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100