Skip to content

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

Open
@anarchvile

Description

@anarchvile

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)!

Metadata

Metadata

Assignees

No one assigned

    Labels

    a-commentsbugPanic, non-idempotency, invalid code, etc.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions