rust-lang / rust-lang/rustfmt

`format_macro_matchers` unusual behavior with commas

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

Nobody has claimed this yet.

A-macros I-poor-formatting only-with-option
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

For a struct-like macro:

macro_rules! foo {
    (
        foo: $foo:literal,
        bar: $bar:literal,
        baz: $baz:literal,
        qux: $qux:literal,
        quux: $quux:literal,
        corge: $corge:literal,
    ) => {};
}

Formatting with format_macro_matchers = true produces weird results:

macro_rules! foo {
    (
        foo:
        $foo:literal,bar:
        $bar:literal,baz:
        $baz:literal,qux:
        $qux:literal,quux:
        $quux:literal,corge:
        $corge:literal,
    ) => {};
}

It seems like it always removes whitespace after a comma rather than using it as a possible break location. Single-line macros have similarly weird results:

macro_rules! foo {
    (foo: $foo:ident,bar: $bar:ident,baz: $baz:ident,qux: $qux:ident,) => {};
}

rustfmt 1.7.0-nightly (f704f3b9 2023-12-19)

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 multiline and single-line macro examples with format_macro_matchers=true, then trace the macro matcher formatting path in rustfmt. Done means commas are treated as possible break locations rather than having following whitespace removed, and both examples produce sensible formatting.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
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.