Skip to content

Missing trailing comma in struct causes subsequent comment to be moved #4654

@vallentin

Description

@vallentin

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 (0f29ff6 2020-11-11)
  • Tested against master (d11fde8)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions