Skip to content

Add rustfmt support for attribute on struct literal field #41392

Closed
@joshlf

Description

@joshlf

rustfmt is currently unable to format code which uses the new (and unstable) attributes on struct fields feature. Demonstration:

$ echo '
> struct Foo {
>     foo: usize,
>     #[cfg(feature = "include-bar")]
>     bar: usize,
> }
> 
> fn new_foo() -> Foo {
>     Foo { 
>         foo: 0,
>         #[cfg(feature = "include-bar")]
>         bar: 0,
>     }
> }
> ' | rustfmt
error: expected identifier, found `#`
  --> stdin:14:9
   |
14 |         #[cfg(feature = "include-bar")]
   |         ^

Interestingly, the same code on the Rust playground is simply formatted to remove the #[cfg(feature = "include-bar")] annotation, producing:

struct Foo {
    foo: usize,
    #[cfg(feature = "include-bar")]
    bar: usize,
}

fn new_foo() -> Foo {
    Foo { foo: 0, bar: 0 }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions