rust-lang / rust-lang/rust

attributes on expressions end up with confusing line breaks

Open
#152,765 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

F-stmt_expr_attributes I-style-radar T-style
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the two expression-attribute examples with rustfmt on stable, then read the linked Zulip thread for the existing formatting rationale. Done means agreeing on and implementing clearer placement for short expression attributes while keeping lambda attributes on a separate line, with formatting coverage for these cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.