rust-lang / rust-lang/rustfmt

Comma gets duplicated when obstructed by inline comments

Open
#6,759 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-comments C-bug I-invalid-code
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

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 https://github.com/rust-lang/rustfmt/issues/3876#issuecomment-619383696 - also reproducible using the current rustfmt tool.


Version

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the examples with cargo run --bin rustfmt -- --version and the supplied Rust input. Trace how rustfmt handles commas adjacent to inline comments, then add coverage for the shown cases and confirm formatting moves commas without duplicating them.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.