Add Support for sorting `#[derive(...)]`
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 7k
- Forks
- 1.1k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 24
Description
Hi rustfmt team,
This idea has come up regularly since 2017 — users keep asking for #[derive(...)] traits to be sorted automatically, just like imports. And each time, it’s been closed with the same core concern: that the order of derive macros might matter, and reordering them could break things.
https://github.com/rust-lang/rustfmt/issues/1867, https://github.com/rust-lang/rustfmt/issues/4112, https://github.com/rust-lang/style-team/issues/154
That concern made sense back then. But a lot has changed since.
To work around the lack of native support, several community tools have emerged:
keepsorted– my own tool to keep blocks like derives sorted. And https://sort-derive-traits.github.io/cargo-derivefmtcargo-sort-derives
These tools are helpful, but they come with limitations. They usually operate via regex or simple token logic, which means they can't match the accuracy or safety of a proper syntax-aware formatter. More importantly, each tool may use a slightly different sorting convention, which introduces fragmentation and inconsistency across projects and teams.
Having this behavior built into rustfmt would solve both problems:
- It would ensure consistency across the Rust ecosystem.
- It would offer users a reliable, idiomatic way to enforce ordering — just like how import sorting works today.
In the last relevant discussion https://github.com/rust-lang/style-team/issues/154#issuecomment-2537594320, it was acknowledged that most of the original blockers no longer apply. The one remaining edge case — where one derive macro implicitly enables another — is:
- Extremely rare
- Easy to detect and fix (just add an import)
- A hard error at compile time, not a subtle bug
Given this, I believe it’s now safe and timely to reconsider.
Would you be open to adding support for sorting derive traits in rustfmt?
Or if there’s still a concrete reason this can’t be done safely, could you share a specific example?
Thanks again for all your work on rustfmt — it’s a tool we use and appreciate every day.
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
The issue names no rustfmt files, tests, or entry points; begin by reviewing the linked prior discussions and related tools to establish the ordering and safety requirements. Done means rustfmt has an agreed, syntax-aware behavior for sorting #[derive(...)] traits, with corresponding implementation coverage and tests.
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
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100