rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
6.05k stars 888 forks source link

Bad formatting with closures #6382

Open Jasonshengxi opened 3 weeks ago

Jasonshengxi commented 3 weeks ago

Formatting the following code produces awful results:

fn main() {
    {
        {
            {
                let result = this_is_a_quite_long_name(
                    another_long_one, 
                    &|from, to| -> LongResult<Words> {
                        () // this comment is to keep it from collapsing
                    }
                )?;
            }
        }
    }
}

it's formatted into this:

fn main() {
    {
        {
            {
                let result = this_is_a_quite_long_name(another_long_one, &|from,
                                                                           to|
                 -> LongResult<
                    Words,
                > {
                    () // this comment is to keep it from collapsing
                })?;
            }
        }
    }
}

playground: link

I can ignore it by using #[rustfmt::skip] but this behavior seems incorrect.

ytmimi commented 3 weeks ago

Likely a duplicate of one of these other closure formatting issues: https://github.com/rust-lang/rustfmt/issues?q=is%3Aissue%20state%3Aopen%20label%3Aa-closures