Skip to content

Lambda with long argument list should be formatted without indent_style="Visual" #5284

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
KarelPeeters opened this issue Mar 31, 2022 · 4 comments · May be fixed by #5285
Closed

Lambda with long argument list should be formatted without indent_style="Visual" #5284

KarelPeeters opened this issue Mar 31, 2022 · 4 comments · May be fixed by #5285

Comments

@KarelPeeters
Copy link

KarelPeeters commented Mar 31, 2022

I have a couple of lambdas with parameters with complex types, which take up a lot of space. This means rustfmt splits up the parameters over multiple lines, but it does so as if indent_style="Visual". A simple reproducing example, on 1.4.38-nightly (2022-03-19 8d60bf4) (the current rust playground formatter):

fn main() {
    let long_lambda = |x: &mut usize,
                       y: &mut usize,
                       z: &mut usize,
                       w: &mut usize,
                       v: &mut usize,
                       a: &mut usize,
                       b: &mut usize| {
        println!("{}", x);
        println!("{}", y);
        println!("{}", z);
        println!("{}", w);
        println!("{}", a);
        println!("{}", b);
    };

    long_lambda(&mut 1, &mut 2, &mut 3, &mut 4, &mut 5, &mut 6, &mut 7);
}

This is inconsistent with the rest of rustfmt formatting, and doesn't play well with things like IDE type hints.

I think it would be better if lambda parameters were formatted more like indent_style="Block", which is the default elsewhere, eg. function parameters.

fn main() {
    let long_lambda = |
        x: &mut usize,
        y: &mut usize,
        z: &mut usize,
        w: &mut usize,
        v: &mut usize,
        a: &mut usize,
        b: &mut usize
    | {
        println!("{}", x);
        println!("{}", y);
        println!("{}", z);
        println!("{}", w);
        println!("{}", a);
        println!("{}", b);
    };

    long_lambda(&mut 1, &mut 2, &mut 3, &mut 4, &mut 5, &mut 6, &mut 7);
}
@ytmimi
Copy link
Contributor

ytmimi commented Mar 31, 2022

Thanks for the report.

This is inconsistent with the rest of rustfmt formatting, and doesn't play well with things like IDE type hints.

I just want to point out that the rules that dictate how rustfmt is meant to work are laid out in the style guide. The relevant section on closures, does seem to indicate that the formatting should be more consistent with functions.

@ytmimi
Copy link
Contributor

ytmimi commented Mar 31, 2022

confirming that on rustfmt 1.4.38-nightly (63acf900 2022-03-27) given this input:

fn main() {
    let long_lambda = |
        x: &mut usize,
        y: &mut usize,
        z: &mut usize,
        w: &mut usize,
        v: &mut usize,
        a: &mut usize,
        b: &mut usize
    | {
        println!("{}", x);
        println!("{}", y);
        println!("{}", z);
        println!("{}", w);
        println!("{}", a);
        println!("{}", b);
    };

    long_lambda(&mut 1, &mut 2, &mut 3, &mut 4, &mut 5, &mut 6, &mut 7);
}

produces this output:

fn main() {
    let long_lambda = |x: &mut usize,
                       y: &mut usize,
                       z: &mut usize,
                       w: &mut usize,
                       v: &mut usize,
                       a: &mut usize,
                       b: &mut usize| {
        println!("{}", x);
        println!("{}", y);
        println!("{}", z);
        println!("{}", w);
        println!("{}", a);
        println!("{}", b);
    };

    long_lambda(&mut 1, &mut 2, &mut 3, &mut 4, &mut 5, &mut 6, &mut 7);
}

The rustfmt.toml used is the same as the one listed in the rustfmt repo:

error_on_line_overflow = true
error_on_unformatted = true
version = "Two"

@ytmimi
Copy link
Contributor

ytmimi commented Mar 31, 2022

Thanks again for the report. I did some more digging and it seems like this is a duplicate of #3865, and the fix for it was already implemented and merged into a different branch by #3867. This fix needs to be backported into the current mainline rustfmt master branch.

@ytmimi ytmimi added duplicate 1x-backport:pending Fixed/resolved in source but not yet backported to a 1x branch and release labels Mar 31, 2022
@calebcartwright
Copy link
Member

Thank you for the report but let's go ahead and close this as a duplicate

@calebcartwright calebcartwright removed the 1x-backport:pending Fixed/resolved in source but not yet backported to a 1x branch and release label Apr 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants