rust-lang / rust-lang/rustfmt

Feature request: Add an option to place the arguments of function-like attributes on the same line until `attr_fn_like_width` is exceeded.

Open
#6,301 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-feature-request P-low
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

When there are many arguments in the #[derive(...)] attribute, placing one argument per line makes the code vertically long and difficult to read.

Could you consider adding an option to place the arguments on the same line until the attr_fn_like_width is exceeded, as shown in the example below?

Example

Current behaviour (rustfmt 1.7.0-stable (3f5fd8d 2024-08-06)):

use derive_more::{
    Add, AddAssign, AsMut, AsRef, Deref, DerefMut, Div, DivAssign, From, Into, Mul, MulAssign, Neg,
    Rem, RemAssign, Sub, SubAssign,
};

#[repr(transparent)]
#[derive(
    Copy,
    Clone,
    Debug,
    PartialEq,
    PartialOrd,
    Deref,
    DerefMut,
    Neg,
    Add,
    AddAssign,
    Sub,
    SubAssign,
    Mul,
    MulAssign,
    Div,
    DivAssign,
    Rem,
    RemAssign,
    From,
    Into,
    AsRef,
    AsMut,
)]
pub struct Meters(pub f32);

fn main() {}

Expected behaviour (when attr_fn_like_width=70):

use derive_more::{
    Add, AddAssign, AsMut, AsRef, Deref, DerefMut, Div, DivAssign, From, Into, Mul, MulAssign, Neg,
    Rem, RemAssign, Sub, SubAssign,
};

#[repr(transparent)]
#[derive(
    Copy, Clone, Debug, PartialEq, PartialOrd, Deref, DerefMut, Neg,
    Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Div, DivAssign,
    Rem, RemAssign, From, Into, AsRef, AsMut,
)]
pub struct Meters(pub f32);

fn main() {}

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 locating rustfmt's formatting path for function-like attributes and the existing attr_fn_like_width option. Reproduce the issue with the provided derive example, then determine how grouped arguments should wrap at the configured width. Done means the example matches the expected output without regressing other function-like attributes.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.