rust-lang / rust-lang/rustfmt

[Tracking] Comment preservation and formatting issues

Open
#6,688 3 comments 0 reactions 1 assignee View on GitHub

@shulaoda is already working on this.

Since Oct 10, 2025.

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

Description

Summary

Rust allows both line comments (//) and block comments (/* */) to be placed between nearly any tokens in the syntax (e.g., pub(crate /* comment */), trait /* comment */ Name, pub // comment\ntrait). However, rustfmt currently struggles to preserve or correctly handle comments in many of these positions, leading to:

Root Cause

Rust's flexible comment placement allows comments between virtually any tokens (as demonstrated in #6671 💀):

pub(crate /* .. */) /* .. */ unsafe /* .. */ auto /* .. */ trait /*..*/ Id /*..*/: /*..*/ Bound /*..*/ + Bound /* .. */ {
    // ...
}

Rustfmt's current architecture has insufficient capabilities for handling comments in all possible positions. The current approach requires manually considering each comment placement scenario and stitching the formatted output together, which is fragile and error-prone.

Current Status

The rustfmt team is aware of these limitations and actively discussing architectural improvements on Zulip. However, comprehensively solving comment formatting across all possible token positions would require significant architectural changes, active contributors with deep understanding of the codebase, and substantial time and effort investment.

If you encounter comment formatting issues: Please report it with a minimal reproducible example and use conventional comment positions as a workaround. Simpler cases may be fixed individually over time, while comprehensive support requires long-term architectural improvements.

Related Issues

Please feel free to modify the description to make it easier to understand or more accurate. If it's not necessary, please feel free to close it.

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.