Skip to content
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

Less ideal formatting for struct pattern matching in if let #3560

Open
upsuper opened this issue May 18, 2019 · 3 comments
Open

Less ideal formatting for struct pattern matching in if let #3560

upsuper opened this issue May 18, 2019 · 3 comments

Comments

@upsuper
Copy link

upsuper commented May 18, 2019

Given the following code:

struct Foo {
    something: Option<u32>,
    bar: u32,
}

fn main() {
    let a = Foo {
        something: Some(1),
        bar: 2,
    };
    if let Foo { something: Some(something), .. } = a {
        println!("{}", something);
    }
}

rustfmt formats the if let as

    if let Foo {
        something: Some(something),
        ..
    } = a
    {
        println!("{}", something);
    }

which looks rather unfortunate...

Should probably keep the original form in this case?

@upsuper
Copy link
Author

upsuper commented May 18, 2019

Also I leave the configuration default, which means control_brace_style should be AlwaysSameLine, however it pushes the brace to the next line here, which seems like a bug?

@topecongiro
Copy link
Contributor

control_brace_style only applies to control flows whose pattern consist of a single line. IIRC, if the pattern consists of multiple lines, then rustfmt will put the opening brace on the next line.

@topecongiro topecongiro added this to the 3.0.0 milestone Jun 7, 2020
@remi-dupre
Copy link

Hi!

I have a few additions about this issue:

  1. The same behavior is applied to match arms (or matches! macro, probably any pattern context we can think of).
  2. This actually controlled by setting struct_lit_width (eg. a value of 50 instead of default 28 is enough get the result expected by @upsuper in his example).

However I still think it would make sense to provide a separate settings for actual struct literals and patterns (patterns being usually less readable if they don't fit in a single line, I think this last particular issue make it hard to use advanced pattern matching on struct enum variants with default rustfmt settings).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants