rust-lang / rust-lang/rustfmt

Weird formatting of whitespace in between code and in-line comments

Open
#6,896 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug needs-triage
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Summary

I tried to format this code:

fn foo() {
    [
        (Ty::U32, OperandKind::Reg, OperandKind::Immediate),  // i32, funcref, externref
        (Ty::U32, OperandKind::Slot, OperandKind::Immediate), // i32, f32, funcref, externref
        (Ty::U64, OperandKind::Reg, OperandKind::Slot),       // i32, i64, funcref, externref
        (Ty::U64, OperandKind::Reg, OperandKind::Immediate),  // i64
        (Ty::U64, OperandKind::Slot, OperandKind::Reg),       // i32, i64
        (Ty::U64, OperandKind::Slot, OperandKind::Slot),      // i32, i64, f32, f64, funcref, externref
        (Ty::U64, OperandKind::Slot, OperandKind::Immediate), // i64, f64
        (Ty::F32, OperandKind::Reg, OperandKind::Immediate),  // f32
        (Ty::F32, OperandKind::Reg, OperandKind::Slot),       // f32
        (Ty::F32, OperandKind::Slot, OperandKind::Reg),       // f32
        (Ty::F64, OperandKind::Reg, OperandKind::Slot),       // f64
        (Ty::F64, OperandKind::Slot, OperandKind::Reg),       // f64
    ];
}
Expected behavior

I expected to see this happen: trailing whitespace between code and in-line comments stays the same.

Actual behavior

Instead, this happened: whitespace between code and in-line comments is weirdly changed from 2 to 6 whitespaces.

fn foo() {
    [
        (Ty::U32, OperandKind::Reg, OperandKind::Immediate), // i32, funcref, externref
        (Ty::U32, OperandKind::Slot, OperandKind::Immediate), // i32, f32, funcref, externref
        (Ty::U64, OperandKind::Reg, OperandKind::Slot),      // i32, i64, funcref, externref
        (Ty::U64, OperandKind::Reg, OperandKind::Immediate), // i64
        (Ty::U64, OperandKind::Slot, OperandKind::Reg),      // i32, i64
        (Ty::U64, OperandKind::Slot, OperandKind::Slot), // i32, i64, f32, f64, funcref, externref
        (Ty::U64, OperandKind::Slot, OperandKind::Immediate), // i64, f64
        (Ty::F32, OperandKind::Reg, OperandKind::Immediate), // f32
        (Ty::F32, OperandKind::Reg, OperandKind::Slot),  // f32
        (Ty::F32, OperandKind::Slot, OperandKind::Reg),  // f32
        (Ty::F64, OperandKind::Reg, OperandKind::Slot),  // f64
        (Ty::F64, OperandKind::Slot, OperandKind::Reg),  // f64
    ];
}

Configuration

rustfmt.toml:

imports_granularity = "Crate"
imports_layout = "HorizontalVertical"
edition = "2021" # 2024 does not change anything
match_arm_leading_pipes = "Preserve"

rustfmt cli options used (if applicable):

cargo +nightly fmt
> cargo +nightly fmt --version --verbose
rustfmt 1.9.0-nightly (fb0a5a5a9c 2026-05-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

Reproduce the formatting change with the provided Rust snippet, rustfmt.toml, and cargo +nightly fmt. Compare the expected and actual spacing around inline comments, then inspect rustfmt's comment and whitespace formatting behavior. Done means the behavior is understood and covered by a regression test that preserves the intended spacing.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.