rust-lang / rust-lang/rustfmt

Cargo fails to format a line comment followed by a block comment

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

Nobody has claimed this yet.

A-2024-style-edition A-comments C-bug I-trailing-whitespace S-has-mcve
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Problem

cargo fmt fails to format the following code (playground link):

fn main() {
    todo!() // line comment
    /*
        block comment
    */
}

with this error:

error[internal]: left behind trailing whitespace
 --> /playground/src/main.rs:2:2:1
  |
2 |     
  | ^^^^
  |

The code is formatted like is this (Note the 4 spaces on the added line between the line and the block comments):

fn main() {
    todo!() // line comment
    
    /*
        block comment
    */
}

The actual code before the line comment doesn't seem to matter, only that there is some code on the same line as the comment.

Steps
  1. Write any function that has some code, then a line comment on the same line, then a block comment on the line right afterwards
  2. Run cargo fmt
Possible Solution(s)

No response

Notes

Affects at least stable 1.93.0 and nightly 1.95.0, and affects both Windows and WSL (Ubuntu).

Version
cargo 1.95.0-nightly (fe2f314ae 2026-01-30)
release: 1.95.0-nightly
commit-hash: fe2f314aef06e688a9517da1ac0577bb1854d01f
commit-date: 2026-01-30
host: x86_64-unknown-linux-gnu
libgit2: 1.9.2 (sys:0.20.3 vendored)
libcurl: 8.15.0-DEV (sys:0.4.83+curl-8.15.0 vendored ssl:OpenSSL/3.5.4)
ssl: OpenSSL 3.5.4 30 Sep 2025
os: Ubuntu 24.4.0 (noble) [64-bit]

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 issue with the Rust snippet in the report and running cargo fmt. Trace the formatter's handling of a line comment followed by a block comment; done means cargo fmt completes without the trailing-whitespace error and preserves the comments without inserting invalid whitespace.

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
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.