rust-lang / rust-lang/rustfmt

rustfmt fails when it sees trailing spaces inside of an attribute

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

Nobody has claimed this yet.

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

Description

Manually transferred from rust-lang/rust#120551.
Originally reported by @max-ishere.


cargo fmt fails to format document with #[test_case()] macro when there is a trailing space inside the macro.

Code (<SPACE> is where the space 0x20 is):

#[cfg(test)]
mod tests {
    #[test_case(
        ()<SPACE>
    )]
    fn foo(i: ()) {}
}

Hexdump:

00000000  23 5b 63 66 67 28 74 65  73 74 29 5d 0a 6d 6f 64  |#[cfg(test)].mod|
00000010  20 74 65 73 74 73 20 7b  0a 20 20 20 20 23 5b 74  | tests {.    #[t|
00000020  65 73 74 5f 63 61 73 65  28 0a 20 20 20 20 20 20  |est_case(.      |
00000030  20 20 28 29 20 0a 20 20  20 20 29 5d 0a 20 20 20  |  () .    )].   |
00000040  20 66 6e 20 66 6f 6f 28  69 3a 20 28 29 29 20 7b  | fn foo(i: ()) {|
00000050  7d 0a 7d 0a                                       |}.}.|
00000054

As you can see its just a space.

Error output:

error[internal]: left behind trailing whitespace
  --> file.rs:55:55:11
   |
55 |         ()
   |           ^
   |

warning: rustfmt has failed to format. See previous 1 errors.

A one-liner space after () is ok:

#[test_case(() )]
fn foo_2(i: ()) {}

Expected behavior:

The formatter doesn't break because of the space and because it is the last thing on the line removes it.

Meta

cargo fmt --version:

rustfmt 1.7.0-nightly (75b064d 2023-11-01)

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 failure with the Rust snippet in the issue using cargo fmt, then trace rustfmt's handling of trailing whitespace inside the #[test_case()] attribute. The fix is complete when the multiline example formats successfully and removes the trailing space, while the one-line example continues to format correctly.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.