rust-lang / rust-lang/rustfmt

Trailing commas handling in macro calls

Open
#3,065 3 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

fn main() {
    foo("abcdefghijklmnopqrstuvwxyz".to_string(), "abcdefghijklmnopqrstuvwxyz".to_string());
}

gets, as expected, formatted to:

fn main() {
    foo(
        "abcdefghijklmnopqrstuvwxyz".to_string(),
        "abcdefghijklmnopqrstuvwxyz".to_string(),
    );
}

However,

fn main() {
    assert_eq!("abcdefghijklmnopqrstuvwxyz".to_string(), "abcdefghijklmnopqrstuvwxyz".to_string());
}

gets formatted as:

fn main() {
    assert_eq!(
        "abcdefghijklmnopqrstuvwxyz".to_string(),
        "abcdefghijklmnopqrstuvwxyz".to_string()
    );
}

No trailing comma after the second parameter.

And even worse, function calls in one of the assert_eq! parameters get the same treatment:

fn main() {
    assert_eq!(foo("abcdefghijklmnopqrstuvwxyz".to_string(), "abcdefghijklmnopqrstuvwxyz".to_string()), "abcdefghijklmnopqrstuvwxyz".to_string());
}

gets formatted as:

fn main() {
    assert_eq!(
        foo(
            "abcdefghijklmnopqrstuvwxyz".to_string(),
            "abcdefghijklmnopqrstuvwxyz".to_string()
        ),
        "abcdefghijklmnopqrstuvwxyz".to_string()
    );
}

No trailing comma after the second parameter of foo.

And if you have a trailing comma and a parameter gets shorter, the trailing comma stays.

fn main() {
    assert_eq!(
        foo(
            "abcdef".to_string(),
            "abcdef".to_string(),
        ),
        "abcdefghijklmnopqrstuvwxyz".to_string()
    );
}

gets formatted as:

fn main() {
    assert_eq!(
        foo("abcdef".to_string(), "abcdef".to_string(),),
        "abcdefghijklmnopqrstuvwxyz".to_string()
    );
}

I understand that trailings commas can change the meaning of macros, but it seems assert_eq! is already special cased.

Even more than the trailing comma after assert_eq!'s second parameter, the handling of trailing commas in function calls in one of assert_eq!'s parameter seems to me pretty inconvenient.

Thanks for the hard work on such a useful tool.

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 three assert_eq! examples with rustfmt and compare their handling of trailing commas in macro arguments and nested function calls. Trace the formatting entry point for macro calls, then inspect how assert_eq! is special-cased. Done means the reported examples have consistent trailing-comma behavior without changing macro semantics.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.