rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
6.04k stars 889 forks source link

Comment before a list element is changed to an end-of-line comment for a different element #6132

Open Zalathar opened 7 months ago

Zalathar commented 7 months ago

(playground link)

Input:

fn hello() {
    let _slice = &[
        // First item
        0x0001,

        // Second item
        0x0002,
    ];
    two_arg_fn(
        // First arg
        1,

        // Second arg
        2,
    );
}

fn two_arg_fn(_x: u32, _y: u32) {}

Output:

fn hello() {
    let _slice = &[
        // First item
        0x0001, // Second item
        0x0002,
    ];
    two_arg_fn(
        // First arg
        1, // Second arg
        2,
    );
}

fn two_arg_fn(_x: u32, _y: u32) {}

Here the intent was to have a comment associated with the element on the following line, but rustfmt confusingly turned the comment for the second element into an end-of-line comment for the first element.


$ rustfmt --version
rustfmt 1.7.0-stable (7cf61ebd 2024-03-27)
ytmimi commented 7 months ago

@Zalathar Thanks for the report. I believe this is related to / is a duplicate of #4108.

Zalathar commented 7 months ago

@Zalathar Thanks for the report. I believe this is related to / is a duplicate of #4108.

It's definitely not a duplicate of that issue, and to me it seems only very loosely related.

(There's a good chance it's a duplicate of something, but I couldn't find anything relevant in my searches.)