rust-lang / rust-lang/rustfmt

rustfmt removes comment in macro invocation

Open
#5,695 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

rustfmt will remove the comment in the following macro invocation:

macro_rules! add_my_field {
    {
        $(#[$m:meta])*
        $v:vis struct $name:ident {
            $($var_v:vis $var_name:ident: $var_ty:ty,)*
        }
    } => {
        $(#[$m])*
        $v struct $name {
            $($var_v $var_name: $var_ty,)*
            my_field: u32,
        }
    }
}

add_my_field!(
#[derive(Debug)]
struct MyStruct {
    a: u8,
    b: u16,
}
// this comment will be deleted (it doesn't matter if it's // or /* */)
);

fn main() {
    println!("{:#?}", MyStruct {
        a: 1,
        b: 2,
        my_field: 3,
    })
}

Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c452814ae6ed3ad5042af833d1e0b359

Tested with:

  • rustfmt 1.5.1-stable (fc594f15 2023-01-24)
  • rustfmt 1.5.2-nightly (2023-02-16 9a7cc6c)

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

Reproduce the issue using the macro invocation and compiler versions shown in the report, confirming that the comment is removed. Trace rustfmt's handling of comments inside macro invocations, then add a regression test for this example. Done means formatting preserves the comment, including both line and block comment forms.

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
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.