Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cyqsimon opened this issue Nov 6, 2022 · 2 comments
Closed

Comments

@cyqsimon
Copy link

cyqsimon commented Nov 6, 2022

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

@ytmimi
Copy link
Contributor

ytmimi commented Nov 7, 2022

Thanks for reaching out.

There was some work done in #5509 to expand the docs on single_line_if_else_max_width.

The explicit return seems like an interesting case.

@ytmimi ytmimi added p-low only-with-option requires a non-default option value to reproduce labels Nov 7, 2022
@calebcartwright calebcartwright removed only-with-option requires a non-default option value to reproduce p-low labels Nov 7, 2022
@calebcartwright
Copy link
Member

tl;dr - duplicate of (primary) item #4351 with more detail in the first half of from #4351 (comment). You can set version=Two to get the desired behavior if you're able to work on nightly, otherwise it's something that just has to be accepted due to the stability guarantees

@calebcartwright calebcartwright closed this as not planned Won't fix, can't repro, duplicate, stale Nov 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants