rust-lang / rust-lang/rustfmt

rustfmt attribute to keep match clauses sorted and merged

Open
#4,822 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-feature-request P-low
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

When writing some long matches that map one kind of value to another, it can be extremely helpful to keep the clauses in alphabetically sorted order, as well as merging multiple mappings to the same value.

I'd love to have a rustfmt attribute, along the lines of #[rustfmt::sorted], which when applied to a match would cause rustfmt to keep the clauses sorted and merged.

By making this a dedicated attribute, it can't affect code unless someone opts into it, and by opting in, the user is explicitly stating that the order doesn't semantically matter. Thus, rustfmt doesn't have to prove that sorting doesn't change the meaning of the code; it can rely on the user for that.

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

The issue names no files, tests, or entry points. Start by locating rustfmt's handling of custom attributes and match-expression formatting, then determine how sorted and merged clauses would be specified and validated; done means an opt-in attribute produces the requested ordering without affecting unannotated matches.

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
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.