rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
6.05k stars 888 forks source link

Macro attributes containing consts not formatted correctly #6374

Open musjj opened 1 month ago

musjj commented 1 month ago

A real-world example from nutype:

This gets formatted just fine:

#[nutype(validate(len_char_min = 5, len_char_max = 20))]
struct Email(String);

But this does not:

#[nutype(validate(len_char_min = 5, len_char_max = 20, regex = EMAIL_REGEX))]
struct Email(String);

It looks like that rustfmt is not liking the EMAIL_REGEX here, but I'm not sure why.

Tested on rustfmt 1.8.0-nightly (3ed6e3cc69 2024-10-17).

ytmimi commented 4 weeks ago

Unfortunately this isn't an issue with rustfmt. It's an issue with the compiler. Specifically the ast::Attribute::meta method, which is returning None for your second example.

rustfmt uses the value it gets back from that call here to format the attribute, otherwise it'll leave the attribute unformatted.

https://github.com/rust-lang/rustfmt/blob/96cc01b197f50eeb657128946d02b8f61da2d12d/src/attr.rs#L350