Skip to content

Can't have single line let-else and control braces on next line #5849

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

Open
fedenator opened this issue Jul 20, 2023 · 4 comments
Open

Can't have single line let-else and control braces on next line #5849

fedenator opened this issue Jul 20, 2023 · 4 comments
Labels
only-with-option requires a non-default option value to reproduce

Comments

@fedenator
Copy link

let-else always expands to multiple lines if control_brace_style = "AlwaysNextLine". I don't know if this is a bug or simply not supported.

@calebcartwright
Copy link
Member

When you get a chance could you please share some minimal code snippets, specifically what you were anticipating and what you're actually seeing?

The former in particular isn't clear to me, but I will say that a let-else statement will only be formatted on a single line provided it's possible to put the entire statement on one line. If that's not the case (either due to a non-default config option or due to one or more of the requirements articulated in the style guide not being met) then the multi-line formatting would apply.

@ytmimi ytmimi added only-with-option requires a non-default option value to reproduce needs-mcve needs a Minimal Complete and Verifiable Example labels Jul 21, 2023
@fedenator
Copy link
Author

Sure, this happened when I typed the line

let Some(position) = q_windows.single().cursor_position() else { return };

I use the next config (relevant lines only)

control_brace_style = "AlwaysNextLine"
single_line_let_else_max_width = 200

So the line gets expanded to this

let Some(position) = q_windows.single().cursor_position()
else
{
	return;
};

Though the let else statement fits in one line, it is being formatted to multiple lines. This is not necessarily wrong, given that I'm using control_brace_style = "AlwaysNextLine", but I'm not sure if not respecting single_line_let_else_max_width is intended the behavior. Also I'm wondering if having something like control_brace_style = "NextLineWhen" so only it's not expanded when single_line_let_else_max_width it's not exceeded would be something that should be added.

Note that having control_brace_style = "AlwaysSameLine" does keeps the statement in one line unless the limit it's exceeded, similar to what I want.

@ytmimi ytmimi removed the needs-mcve needs a Minimal Complete and Verifiable Example label Jul 31, 2023
@ytmimi
Copy link
Contributor

ytmimi commented Jul 31, 2023

This is Interesting 🤔. personally I think the current formatting is correct given the use of control_brace_style = "AlwaysNextLine", though checking similar behavior using single_line_if_else_max_width would suggest that the single_line_if_else_max_width value should take precedence.

input:

fn main() {
    if lorem
    {
        println!("ipsum!")
    }
    else
    {
        println!("dolor!")
    }
}

output: (using control_brace_style=AlwaysNextLine,single_line_if_else_max_width=100)

fn main() {
    if lorem { println!("ipsum!") } else { println!("dolor!") }
}

@lukesneeringer
Copy link

It actually looks like control_brace_type = "ClosingNextLine" actually prevents any single-line if-else or let-else block.

For example:

let foo = if true { 0 } else { 1 };

This gets formatted to...

let foo = if true {
    0
}
else {
    1
};

I always wondered why rustfmt didn't allow me to have a single-line if/else (or let/else), but this option is what does it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
only-with-option requires a non-default option value to reproduce
Projects
None yet
Development

No branches or pull requests

4 participants