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

Extra whitespace inserted between literal and line comment. #5826

Open
hackguy25 opened this issue Jul 11, 2023 · 1 comment
Open

Extra whitespace inserted between literal and line comment. #5826

hackguy25 opened this issue Jul 11, 2023 · 1 comment

Comments

@hackguy25
Copy link

When inserting line comments in an array literal, rustfmt sometimes insert unnecessary empty space.

Simplest example:

let example_correct = [
    0, 0, // comment 1
    0, // comment 2
];

let example_incorrect = [
    0, 0x0, // comment 1
    0,   // comment 2
];

Notice how there are three spaces between the trailing comma and the comment 2 in the incorrect example.

Examples can be reproduced on the Rust Playground, which uses rustfmt 1.6.0-nighty (2023-07-10 8ca44ef), as well as with rust-analyzer v0.3.1583 VS Code extension.

@ytmimi
Copy link
Contributor

ytmimi commented Jul 11, 2023

@hackguy25 thanks for reaching out!

rustfmt is trying to align the comments. If you were to manually format 0x0, // comment 1 and move it to the next line you'll see that. Here's what it looks like:

let example_incorrect = [
    0,
    0x0, // comment 1
    0,   // comment 2
];

0 and 0x0, are small enough that rustfmt uses a compressed formatting style when outputting the array. however it incorrectly tries to align comments in this case, and it would probably be best not to align comments if we're not using a vertical layout.

To further observe and highlight the comment alignment you can run rustfmt and set short_array_element_width_threshold=0:

Input:

fn main() {
    let example_correct = [
        0, 0, // comment 1
        0, // comment 2
    ];

    let example_incorrect = [
        0, 0x0, // comment 1
        0,   // comment 2
    ];
}

output:

fn main() {
    let example_correct = [
        0,
        0, // comment 1
        0, // comment 2
    ];

    let example_incorrect = [
        0,
        0x0, // comment 1
        0,   // comment 2
    ];
}

Also want to note that this is probably related to #4108, although that issue is more focused on function arguments.

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

No branches or pull requests

2 participants