Skip to content

arbitrary_source_item_ordering should allow sorting constructors first #14739

Open
@pv42

Description

@pv42

Summary

Add option to expect constructor (new) functions first in impl blocks.

Lint Name

arbitrary_source_item_ordering

Reproducer

I tried this code:

pub struct Foo {
    bar: i32,
}

impl Foo {
    pub fn new(bar: i32) -> Self {
        Self { bar }
    }
    pub fn bar(&self) -> i32 {
        self.bar
    }
}

I saw this happen:

warning: incorrect ordering of items (must be alphabetically ordered)
 --> src/foo.rs:9:12
  |
9 |     pub fn bar(&self) -> i32 {
  |            ^^^
  |
note: should be placed before `new`
 --> src/foo.rs:6:12
  |
6 |     pub fn new(bar: i32) -> Self {
  |            ^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#arbitrary_source_item_ordering

I expected to see this happen:
Nothing.
Static functions called new should be either ignored or expected to be the first item.

Version

rustc 1.88.0-nightly (2e6882ac5 2025-05-05)
binary: rustc
commit-hash: 2e6882ac5be27a73293d6f7ae56397fdf32848de
commit-date: 2025-05-05
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.4

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions