rust-lang / rust-lang/rust-clippy

[lint] Impl Ordering within file

Open
#16,189 0 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

Does this exist already? I'd like it for large files to have standard ordering?

Impls following standard orders e.g. (all alphabetical?)

  1. Inherent
  2. Standard lib
  3. External crate
  4. Custom
Advantage
  • Large files follow standard patterns
Drawbacks

Maybe as an opt in with config, because:

  1. Maybe you dont want this?
  2. Maybe bad for multiple structs in a file?
  3. Impls in other files
  4. It's already somewhere and I'm blind?
  5. Most important functional impls manually still need to be at the top?
Example
struct MyType {
    value: i32,
}

// 1. External crate trait appears first
impl serde::Serialize for MyType { /* ... */ }

// 2. Custom trait appears before standard library traits
impl MyTrait for MyType { /* ... */ }

// 3. Standard library traits are out of alphabetical order
impl Debug for MyType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "MyType({})", self.value)
    }
}

impl Clone for MyType {
    fn clone(&self) -> Self {
        MyType { value: self.value }
    }
}

// 4. Inherent impl appears last
impl MyType {
    fn new(value: i32) -> Self {
        MyType { value }
    }
}

Could be written as:

struct MyType {
    value: i32,
}

// 1. Inherent impl
impl MyType {
    fn new(value: i32) -> Self {
        MyType { value }
    }
}

// 2. Standard library traits (alphabetical)
impl Clone for MyType {
    fn clone(&self) -> Self {
        MyType { value: self.value }
    }
}

impl Debug for MyType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "MyType({})", self.value)
    }
}

// 3. External crate traits (alphabetical)
impl serde::Serialize for MyType { /* ... */ }

// 4. Custom traits (alphabetical)
impl MyTrait for MyType { /* ... */ }
Comparison with existing lints

No response

Additional Context

No response

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 files, tests, or entry points are named. Start by checking whether an existing Clippy lint supports ordering impl blocks, then define the ordering rules and configuration behavior; done means the requested ordering is consistently checked with clear handling for the listed edge cases.

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
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.