rust-lang / rust-lang/rustfmt

Add option to break after = in assignment

Open
#3,514 7 comments 12 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

I would like to see an option in rustfmt to break after the = in an assignment when the right-hand-side is "complex". I admit I don't know what "complex" is — possibly if the right-hand-side is subject to block/visual formatting. Here are a couple examples:

let var1 = if condition {
    true_part
} else {
    false_part
};

let var2 = some_value
    .chained()
    .method()
    .calls();

To me, it would be nicer to see this as:

let var1 =
    if condition {
        true_part
    } else {
        false_part
    };

let var2 =
    some_value
        .chained()
        .method()
        .calls();

In both cases, it seems more readable to me to keep the block together at the same indent level, especially when the variable name gets long.

let it_gets_harder_to_see_them_together = if condition {
    true_part
} else {
    false_part
};

let some_value_winds_up_in_right_field = some_value
    .chained()
    .method()
    .calls();

Compare

let it_gets_harder_to_see_them_together =
    if condition {
        true_part
    } else {
        false_part
    };

let some_value_winds_up_in_right_field =
    some_value
        .chained()
        .method()
        .calls();

You can also replace long variable names with patterns or reasonable variables with type hints.

let GetsHarder { to_see: them_together } = if condition {
    true_part
} else {
    false_part
};

let var1: GetsHarder<ToSee, ThemTogether> = if condition {
    true_part
} else {
    false_part
};

To be clear, I want to keep block formatting. I just would like the option to have the block start indented on the next line.

PS. I didn't want to include this as a motivating example, because it's hardly rustfmt's fault, but editors like IntelliJ will insert "smart" type hints inline automatically. This pads out the line length in a way that rustfmt can't see, and it's why I don't suggest basing this on line length, but rather on whether the right-hand-side is "complex", block-formatted, or something like that.

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 assignment formatting and existing configuration options. Clarify how a complex or block-formatted right-hand side should be identified and how the option interacts with line length. Done means the provided assignment, conditional, chained-call, pattern, and type-hint examples format with the right-hand side on the next indented line while preserving block formatting.

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
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.