fmt-rfcs icon indicating copy to clipboard operation
fmt-rfcs copied to clipboard

attributes on expressions end up with confusing line breaks

Open programmerjake opened this issue 1 month ago • 2 comments

Thread on Zulip Some code I'm working on right now that ends up with terrible formatting when using rustfmt on stable: (note the code is in an attribute macro, so it works just fine on stable.)

let new_entry = #[hdl(sim)]
BTBEntry {
    start_pc,
    target_pc: target_pc.unwrap_or(0u64),
    fallthrough_offset,
    insn_kind,
    addr_kind,
};
let entry_mut = &mut this.branch_target_buffer.branch_pc_to_target_map[btb_entry_index];
*entry_mut = #[hdl(sim)]
HdlSome(new_entry);

It was previously stated that the formatting rules for attributes is designed for items, not expressions:

@joshtriplett said:

That style guide provision was written specifically to apply to "items", not to expressions.

So I think we should change it to work better with expressions. I propose: Put short attributes on the same line for expression attributes, except for lambdas, so attributes on lambdas would still be on a separate line, but attributes for things like #[a] a + #[b] b could be on the same line

programmerjake avatar Nov 28 '25 09:11 programmerjake