rust-lang / rust-lang/rustfmt

`use_small_heuristics = "Max"` ignores `max_width` for macros

Open
#6,538 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-2024-style-edition A-macros I-max-width
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Take the following Rust code:

fn main() {
    println!("Hello, world!");
}

macro_rules! trigger {
    (KeyBinding, $key:literal, $location:expr) => {{ BindingKey::Keycode { key: Key::Character($key.into()), location: $location } }};
    (KeyBinding, $key:literal,) => {{ BindingKey::Keycode { key: Key::Character($key.into()), location: KeyLocation::Any } }};
    (KeyBinding, $key:ident, $location:expr) => {{ BindingKey::Keycode { key: Key::Named(NamedKey::$key), location: $location } }};
    (KeyBinding, $key:ident,) => {{ BindingKey::Keycode { key: Key::Named(NamedKey::$key), location: KeyLocation::Any } }};
    (MouseBinding, $base:ident::$button:ident,) => {{ $base::$button }};
}

enum Key {
    Named(NamedKey),
    Character(char),
}

enum NamedKey {}

enum KeyLocation {
    Any,
}

When formatted with the default formatter, this works properly and fixes the overly long lines in the macro.

However when then switching to the following rustfmt.toml:

use_small_heuristics = "Max"
max_width = 100 # Optional, just for clarity

Suddenly the macro match arms are all reformatted to a single line, even though use_small_heuristics = "Max" is supposed to max out at max_width and not just allow infinitely long lines.

I've had this break one of my macros during the 2024 edition migration, so I believe there might be a regression somewhere?

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 with the Rust reproducer and the shown rustfmt.toml, then run rustfmt with default settings and with use_small_heuristics = "Max" and max_width = 100 to compare the macro formatting. Done means the Max configuration still wraps the macro match arms without producing lines beyond max_width.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.