rust-lang / rust-lang/rustfmt

Should not visual-align same-line comments within function arguments

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

Nobody has claimed this yet.

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

Description

Sample code:

fn main() {
    w(
        thing,
        0, // thing2
        thing3,
        long_thing4, // acomment
        long_thing4, // bcomment
        long_thing4, // ccomment
        0, // another
        0, // comment
        thing5,
        long_thing6,
        thing7,
        0, // yet another comment
    );
}

rustfmt turns this into:

fn main() {
    w(
        thing,
        0, // thing2
        thing3,
        long_thing4, // acomment
        long_thing4, // bcomment
        long_thing4, // ccomment
        0,           // another
        0,           // comment
        thing5,
        long_thing6,
        thing7,
        0, // yet another comment
    );
}

Notice that // another and // comment got indented to visually align with the previous three comments.

This has multiple issues:

  1. rustfmt should not, in general, be visually aligning anything; its defaults normally block-align everything, per https://github.com/rust-dev-tools/fmt-rfcs/issues/8 .
  2. In this specific case, rustfmt has made the code less readable by doing so, moving comments away from the code they comment (0,) to align it with an unrelated comment, while leaving other comments in the same function call untouched. The alignment misleadingly suggests some relationship between the comments or the arguments, which doesn't exist.

I would expect rustfmt to always leave same-line comments one space away from the code they comment.

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 issue's Rust sample with rustfmt and compare the shown output. Trace the formatter's same-line comment alignment behavior, then verify that comments remain one space from their associated code without aligning to unrelated comments. Add or update coverage for this example and run the relevant rustfmt tests.

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
Quiet
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.