rust-lang / rust-lang/rustfmt

Possible suboptimal formatting on `if let` with multiple patterns

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

Nobody has claimed this yet.

A-patterns C-discussion
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

The indentation of the second pattern in these functions (especially the first one) looks kind of odd. I can look into fixing it if it should be fixed.

pub fn f(input_value: Test) -> (u8, u8) {
    let intermediate = if let Test::OptionWithLongNameA { value1, value2, .. }
    | Test::OptionWithLongNameB { value1, value2, .. } = input_value
    {
        (*value1, *value2)
    } else {
        unreachable!()
    };
    intermediate
}

pub fn g(input_value: Test) -> (u8, u8) {
    let very_very_long_name_for_intermediate =
        if let Test::OptionWithLongNameA { value1, value2, .. }
        | Test::OptionWithLongNameB { value1, value2, .. } = input_value
        {
            (*value1, *value2)
        } else {
            unreachable!()
        };
    very_very_long_name_for_intermediate
}

pub fn h(long_input_value_name: Test) -> (u8, u8) {
    if let Test::OptionWithLongNameA { value1, value2, .. }
    | Test::OptionWithLongNameB { value1, value2, .. } = long_input_value_name
    {
        (*value1, *value2)
    } else {
        unreachable!()
    }
}

enum Test {
    OptionWithLongNameA { value1: u8, value2: u8, value3: u8 },
    OptionWithLongNameB { value1: u8, value2: u8, value3: u8 },
    NeverUsed1,
}

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 running rustfmt on the Rust examples in this issue and compare the indentation of the second pattern across functions f, g, and h. Trace the formatter behavior responsible for these pattern layouts; done means the multiple-pattern if let formatting is consistently improved without regressing the examples.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.