Skip to content

Add option to break after = in assignment #3514

Open
@chrisbouchard

Description

@chrisbouchard

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions