Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Maxpxt opened this issue Oct 24, 2021 · 1 comment · Fixed by #5141
Closed

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

Maxpxt opened this issue Oct 24, 2021 · 1 comment · Fixed by #5141

Comments

@Maxpxt
Copy link

Maxpxt commented Oct 24, 2021

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)

@ytmimi
Copy link
Contributor

ytmimi commented Oct 26, 2021

Thanks for the report!

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.

The comment jumping around due to a trailing comma is related to #4848

Right now I'm not totally sure about the behavior you mentioned with two trailing comments or the reason why a trailing comma at the end of the last comment would prevent reformatting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants