Skip to content

single line if-else not appied if expression is used as implicit return #5593

Closed as not planned
@cyqsimon

Description

@cyqsimon

TLDR:

// single line expected; got multi line
fn is_zero(n: i32) -> bool {
    if n == 0 {
        true
    } else {
        false
    }
}

To reproduce:

Create a new cargo project with default rustfmt settings, paste in this function, and format document.

Expected:

fn is_zero(n: i32) -> bool {
    if n == 0 { true } else { false }
}

Actual:

fn is_zero(n: i32) -> bool {
    if n == 0 {
        true
    } else {
        false
    }
}

Misc:

Adding explicit return triggers single line mode, which shows it's not related to single_line_if_else_max_width:

fn is_zero(n: i32) -> bool {
    return if n == 0 { true } else { false };
}

Versions:

rustfmt 1.5.1-stable (897e375 2022-11-02) on Linux 6.0.6 x86_64

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions