Skip to content

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

Closed
@KarelPeeters

Description

@KarelPeeters

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);
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions