rust-lang / rust-lang/rustfmt

if-else inside match arm always wraps

Open
#6,707 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-matches E-needs-mcve
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

The following code

let prefix = match power {
    0 => "",
    1 => if binary { "K" } else { "k" },
    _ => &"MGTPEZYRQ"[power - 2..][..1],
};

is always formatting as the following

let prefix = match power {
    0 => "",
    1 => {
        if binary {
            "K"
        } else {
            "k"
        }
    }
    _ => &"MGTPEZYRQ"[power - 2..][..1],
};

There doesn't appear to be any option to keep the second match arm on a single line. (use_small_heuristics = "Max", max_width = 9999, match_arm_blocks = false and single_line_if_else_max_width = 9999 are not effective.)

I am testing with the following config with the nightly toolchain:

edition = "2024"
empty_item_single_line = true
fn_single_line = true
format_strings = true
group_imports = "StdExternalCrate"
hex_literal_case = "Lower"
imports_granularity = "Module"
match_arm_blocks = false
max_width = 9999
overflow_delimited_expr = true
reorder_impl_items = true
single_line_if_else_max_width = 9999
single_line_let_else_max_width = 9999
style_edition = "2024"
trailing_semicolon = false
unstable_features = true
use_field_init_shorthand = true
use_small_heuristics = "Max"
use_try_shorthand = true

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

The payload names no files or tests; first reproduce the provided Rust snippet with the nightly toolchain and configuration. Trace the rustfmt handling of match arms and if-else expressions, and consider the issue complete when the example preserves the second arm on one line without regressing related formatting behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.