Cargo fails to format a line comment followed by a block comment
Nobody has claimed this yet.
- 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
- Write any function that has some code, then a line comment on the same line, then a block comment on the line right afterwards
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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