rust-lang / rust-lang/rustfmt

Comments placed in-between a field and type in a struct get deleted after formatting

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

Nobody has claimed this yet.

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

Description

System/Tool Info
  • Ubuntu 22.04.
  • Rust 2024 edition.
rustup toolchain list
stable-x86_64-unknown-linux-gnu (default)
1.85.1-x86_64-unknown-linux-gnu
1.86.0-x86_64-unknown-linux-gnu (active)

cargo fmt --version
rustfmt 1.8.0-stable (05f9846f89 2025-03-31)

cargo --version
cargo 1.86.0 (adf9b6ad1 2025-02-28)

Repro Steps

I wrote the following snippet of code:

#[derive(Debug)]
struct MyCustomStruct {
    my_really_long_name_with_a_long_type:
        std::sync::Arc<std::sync::Mutex<std::collections::HashSet<String>>>,
}

If I try to add a comment here:

#[derive(Debug)]
struct MyCustomStruct {
    my_really_long_name_with_a_long_type:
    // A comment.
        std::sync::Arc<std::sync::Mutex<std::collections::HashSet<String>>>,
}

And then run cargo fmt, the comment gets deleted and the struct looks like the 1st code snippet again. Not sure whether or not it's expected...

Am happy to provide more details, and apologies if something has already been filed for this (skimming the open issues I saw a few similar, but not quite-the-same, items, but I may have just missed 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.

Research direction

Start by reproducing the example with cargo fmt on the listed Rust toolchain, then trace rustfmt's handling of comments between a struct field name, its type, and the type's indentation. Confirm the expected preservation behavior and add a regression test demonstrating whether the comment remains after formatting.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.