Skip to content

rustfmt fails when it sees trailing spaces inside of an attribute #6054

Open
@fmease

Description

@fmease

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions