rust-lang / rust-lang/rustfmt

Non-idempotence for macros in match arms

Open
#5,204 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

The following example takes two formatting attempts to settle, and furthermore settles into a horrible state:

fn x() {
    match 3 {
        _ => return println!("String long enough to reach `max_width + 13`......................................"),
    }
}

formats once to:

fn x() {
    match 3 {
        _ => return println!(
            "String long enough to reach `max_width + 13`......................................"
        ),
    }
}

and a second time to:

fn x() {
    match 3 {
        _ => {
            return println!(
            "String long enough to reach `max_width + 13`......................................"
        )
        }
    }
}

It seems that the second transformation is where the bug lies - the first formatting is what I'd expect, and already falls under max_width. Plus, the final formatting just looks awful.

A few things to help anyone tracking this down (which could be me when I get some time):

  • It needs to be a macro, but any macro will do. A function with a name of equivalent length settles after the first step.
  • It needs both the match and return statement, but any match pattern has the same effect.

I'm very happy to work on this when I get some time.

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 reproducing the two formatting passes with the Rust snippet in the issue, then trace rustfmt's handling of macros inside match arms and return statements. Done means formatting this example is idempotent after one pass and does not produce the malformed-looking second output.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.