rust-lang / rust-lang/rustfmt

Option to preserve match alignment

Open
#894 27 comments 89 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Some people like to align match arms for readability

match val {
    &TOMLValue::Integer(_)        => ArrayType::Integer,
    &TOMLValue::Float(_)          => ArrayType::Float,
    &TOMLValue::Boolean(_)        => ArrayType::Boolean,
    &TOMLValue::DateTime(_)       => ArrayType::DateTime,
    &TOMLValue::Array(_)          => ArrayType::Array,
    &TOMLValue::String(_,_)       => ArrayType::String,
    &TOMLValue::InlineTable(_)    => ArrayType::InlineTable,
    &TOMLValue::Table             => panic!("Cannot have a table in an array"),
 }

And currently it gets reformatted like this

match val {
    &TOMLValue::Integer(_) => ArrayType::Integer,
    &TOMLValue::Float(_) => ArrayType::Float,
    &TOMLValue::Boolean(_) => ArrayType::Boolean,
    &TOMLValue::DateTime(_) => ArrayType::DateTime,
    &TOMLValue::Array(_) => ArrayType::Array,
    &TOMLValue::String(_, _) => ArrayType::String,
    &TOMLValue::InlineTable(_) => ArrayType::InlineTable,
    &TOMLValue::Table => panic!("Cannot have a table in an array"),
}

It would be nice if rustfmt offered an option like match_align_arms that can have multiple values:

  • Always: rustfmt will try to align all (non-block) match arms
  • Preserve: rustfmt will preserve alignment if it is already aligned manually. For matches that are not already aligned, current behavior will be used.
  • Never: current behavior

I think Preserve should be the default behavior.

/cc @joelself

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

Use the aligned and currently reformatted match-arm examples as the behavioral reference. Locate rustfmt’s match-arm formatting and configuration handling, then determine how Always, Preserve, and Never should interact with existing behavior. Done means the option semantics are implemented and covered by formatting tests, though this issue names no specific files or 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
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.