Skip to content

Strange formatting for closure with match #1395

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
jonhoo opened this issue Mar 21, 2017 · 7 comments
Closed

Strange formatting for closure with match #1395

jonhoo opened this issue Mar 21, 2017 · 7 comments

Comments

@jonhoo
Copy link
Contributor

jonhoo commented Mar 21, 2017

Take the following (manually formatted) code:

fn main() {
    let bar = Some(true);
    let foo = Some(true);
    let mut x = false;
    bar.and_then(|_| {
        match foo {
            None => None,
            Some(b) => {
                x = true;
                Some(b)
            }
        }
    });
}

rustfmt will currently turn that into

fn main() {
    let bar = Some(true);
    let foo = Some(true);
    let mut x = false;
    bar.and_then(|_| match foo {
                     None => None,
                     Some(b) => {
        x = true;
        Some(b)
    }
                 });
}

Which is much less visually clear. It almost seems like rustfmt forgets to carry along the indentation level?

@jonhoo
Copy link
Contributor Author

jonhoo commented Mar 21, 2017

I wonder if this is just another manifestation of #1332...

@aldanor
Copy link

aldanor commented Mar 27, 2017

#1405 could be the the same thing?

@jonhoo
Copy link
Contributor Author

jonhoo commented Mar 27, 2017

@aldanor in fact, I think these are all the result of the new visual indent rules for closures (in #1332). If you look at these cases, rustfmt does "the right thing" for all of them, in the sense of choosing to use visual indent for the closure body, and then block closure for some things inside the closure body. The bug seems to be that block indent inside a visual indent does not inherit the indentation level of the containing visual indent.

@nrc
Copy link
Member

nrc commented Apr 5, 2017

With the RFC style, this is currently formatted as:

fn main() {
    let bar = Some(true);
    let foo = Some(true);
    let mut x = false;
    bar.and_then(
        |_| match foo {
            None => None,
            Some(b) => {
                x = true;
                Some(b)
            }
        },
    );
}

Which is much better, but not quite perfect.

@aldanor
Copy link

aldanor commented Apr 6, 2017

Better indeed. However match lining up with the match arms (None and Some) is particularly unfortunate, since |_| happens to take exactly 4 spaces, same as the tab width...

In the original example, closure body was surrounded by braces -- were they removed by rustfmt?

@nrc
Copy link
Member

nrc commented Apr 6, 2017

In the original example, closure body was surrounded by braces -- were they removed by rustfmt?

Yes

@topecongiro
Copy link
Contributor

Closed by #1454.

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