rust-lang / rust-lang/rustfmt

Missing trailing comma in struct causes subsequent comment to be moved

Open
#4,654 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Both in relation to defining and instantiating structs, if the last field does not have a trailing comma, then rustfmt inserts one. However, if a comment is subsequently present, then it is moved up as well.

If the comma was already present, then the comment is not moved.

Before:

struct Foo {
    bar: ()
    // Comment
}

struct Bar {
    baz: ()
    /*
    Comment
    */
}

struct Baz(
    ()
    // Comment
);

fn main() {
    let _ = Foo {
        bar: ()
        /*
        Comment
        */
    };

    let _ = Bar {
        baz: ()
        /*
        Comment
        */
    };

    let _ = Baz(
        ()
        // Comment
    );
}

After:

struct Foo {
    bar: (), // Comment
}

struct Bar {
    baz: (), /*
             Comment
             */
}

struct Baz(
    (), // Comment
);

fn main() {
    let _ = Foo {
        bar: (), /*
                 Comment
                 */
    };

    let _ = Bar {
        baz: (), /*
                 Comment
                 */
    };

    let _ = Baz(
        (), // Comment
    );
}

  • rustfmt 1.4.25-stable (0f29ff6d 2020-11-11)
  • Tested against master (d11fde864d2369623de35a951b8a2026b7652b20)

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

No source file or test is named. Start by reproducing the supplied struct-definition and struct-instantiation examples with rustfmt, then trace formatting and comment handling for missing trailing commas. Done means comments remain in their original relative position while the required commas are inserted.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.