rust-lang / rust-lang/rust-clippy

Flag duplicate constraints

Open
#8,176 1 comment 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

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 impl OR 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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.