Skip to content

Comma gets duplicated when obstructed by inline comments #6759

@bczhc

Description

@bczhc

Input:

fn foo() {
    struct A {
        a: i32 /* i32 */, /* type */
    }

    let a = A {
        a: 123 /* default */, /* value */
    };

    let b = match () {
        () => 123 /* some */, /* comment */
    };

    match todo!() {
        Ok(0) => break /* EOF */,
        Ok(r) => read += r,
        Err(e) if e.kind() == ErrorKind::Interrupted => continue,
        Err(e) => return Err(e),
    }
}

// https://github.com/rust-lang/rustfmt/issues/3876#issuecomment-619383696
fn f<S>(s: S)
where
    S: Send /* */,
             //
{
}

Output:

fn foo() {
    struct A {
        a: i32, /* i32 */, /* type */
    }

    let a = A {
        a: 123, /* default */, /* value */
    };

    let b = match () {
        () => 123, /* some */, /* comment */
    };

    match todo!() {
        Ok(0) => break, /* EOF */,
        Ok(r) => read += r,
        Err(e) if e.kind() == ErrorKind::Interrupted => continue,
        Err(e) => return Err(e),
    }
}

// https://github.com/rust-lang/rustfmt/issues/3876#issuecomment-619383696
fn f<S>(s: S)
where
    S: Send, /* */,
             //
{
}

Note the difference on /* */ lines! An unexpected comma was added.

Expected:

Might be something like a: i32, /* i32 */ /* type */ or Ok(0) => break, /* EOF */? By the code style convention comma gets moved (but not unexpectedly duplicated) before the inline comments.

Maybe an uncovered case or regression of #3876? I took the example in #3876 (comment) - also reproducible using the current rustfmt tool.


Version

cargo run --bin rustfmt -- --version
rustfmt 1.8.0-nightly (86261bfb87 2025-12-08)

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triageThis issue or PR needs triaging to determine its status. Remove label once sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions