rust-lang / rust-lang/rustfmt

rustfmt formats a macro when it should not

Open
#6,418 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

rustfmt is formatting - changing indentation - inside a macro, in what is not valid Rust code.

In the code below, if you change the indentation inside the proptest! macro, rustfmt will change it back to the way it is currently, when it should not. Repo with the repro.

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Item {
    pub first_long_name_a: u16,
    pub item_count: u16,
    pub area: u16,
    pub address: u16,
}

#[cfg(test)]
mod tests {
    use proptest::prelude::*;

    use super::Item;

    proptest! {
               #[test]
               fn read_item_chars(s in "\\PC*") {
                   let pi = Item{
    first_long_name_a: 0,item_count: 0,area: 0,address: 0,};
               }
           }
}

Tested on both 1.82 and nightly.

$ cargo fmt --version
rustfmt 1.7.1-stable (f6e511e 2024-10-15)
$ cargo +nightly fmt --version
rustfmt 1.8.0-nightly (33c245b9e9 2024-12-10)

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 behavior in the linked proptest repository and running the supplied stable and nightly cargo fmt versions. Done means rustfmt leaves indentation inside the proptest! macro unchanged when that content is not valid Rust code.

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.