rust-lang / rustfmt

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

Separate function parameter from its attributes by a newline #6276

Open Veetaha opened 2 months ago

Veetaha commented 2 months ago

Context

Hi, I'm the author of the crate bon. It exposes a proc macro that generates a builder for a function. Every function parameter can be configured with additional #[builder(...)] options.

Problem

For example, this function declares 4 parameters, two of which are optional (they have default values). As for me, the way rustfmt formatted this code doesn't look nice:

#[bon::builder]
fn example(
    #[builder(default = 1)] foo: u32,
    bar: u32,
    #[builder(default = 3)] baz: u32,
    fizz: u32,
) {
}

The attribute on the function's parameter was formatted on the same line with the parameter itself, even though the signature of the function already takes up multiple lines. It is harder to read the signature this way because names of function parameters aren't aligned

I'd expect the following formatting for this example of code instead:

#[bon::builder]
fn example(
    #[builder(default = 1)]
    foo: u32,
    bar: u32,
    #[builder(default = 3)]
    baz: u32,
    fizz: u32,
) {
}
ytmimi commented 2 months ago

Maybe we need to extend inline_attribute_width (https://github.com/rust-lang/rustfmt/issues/3343) to also apply to function parameters?

Veetaha commented 2 months ago

Maybe we need to extend inline_attribute_width (https://github.com/rust-lang/rustfmt/issues/3343) to also apply to function parameters?

It definitely makes sense, but could it just be the default? I mean that the attribute isn't placed on the same line in function arguments?

ytmimi commented 2 months ago

rustfmt can't change default formatting because of it's stability guarantee. Breaking changes can only be made if they're gated.

Veetaha commented 2 months ago

I see, well, at least the default can be changed across editions, but having a config override in the meantime is reasonable

ytmimi commented 2 months ago

Any change to leverage inline_attribute_width would need to be gated since the default value for the option is 0, which would force the wrapping behavior that you're looking for.