rust-lang / rust-lang/rust-clippy

`arbitrary_source_item_ordering` should allow sorting constructors first

Open
#14,739 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-feature-request
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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

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

Start by locating the implementation and tests for the arbitrary_source_item_ordering lint. Reproduce the shown impl Foo ordering, then verify that a new constructor is accepted first or ignored as requested, with tests covering the resulting behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.