Skip to content

Comment with a trailing comma in a function call prevents formatting #5042

@Maxpxt

Description

@Maxpxt

rustfmt does not format a function call whose last argument does not have a trailing comma and is followed by two or more comments, with the last comment ending in a comma. E.g.:

fn main() {
    let _ = std::ops::Add::add(10, 20
        // ...
        // ...,
        );
}

is left unchanged by rustfmt.

When the last comment does not have a comma, rustfmt works correctly, although the positioning of the first comment might be questionable:

fn main() {
    let _ = std::ops::Add::add(10, 20
        // ...
        // ...
        );
}

becomes

fn main() {
    let _ = std::ops::Add::add(
        10, 20, // ...
           // ...
    );
}

When a single comment is present, rustfmt works correctly, with the positioning of the comment depending on whether there is a trailing comma after the last argument:

fn main() {
    let _ = std::ops::Add::add(
        10, 20, // ...
           // ...
    );
}

When the last argument has a trailing comma, rustfmt works correctly:

fn main() {
    let _ = std::ops::Add::add(10, 20
        // ...,
        );
}

becomes

fn main() {
    let _ = std::ops::Add::add(
        10, 20, // ...,
    );
}

and

fn main() {
    let _ = std::ops::Add::add(10, 20,
        // ...,
        );
}

becomes

fn main() {
    let _ = std::ops::Add::add(
        10, 20,
        // ...,
    );
}

rustfmt version: rustfmt 1.4.38-nightly (91b9319 2021-10-23)

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