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

Rustfmt fails to format code #6438

Closed
ImplOfAnImpl opened this issue Jan 8, 2025 · 1 comment
Closed

Rustfmt fails to format code #6438

ImplOfAnImpl opened this issue Jan 8, 2025 · 1 comment

Comments

@ImplOfAnImpl
Copy link

Rustfmt fails to format the following code.
Nothing is printed to the console. However, if I deliberately add a trailing whitespace somewhere, then it starts emitting the "left behind trailing whitespace" error, like in #2896

struct Foo;

impl Foo {
    async fn foo(&mut self) {
        if true {
            async {


                if true {
                    if true {

                        if true {



                                log::info!("A relatively long string 111111111111111111111111111111111111111111");





                        }
                    }
                }

            }
            .await;
        }
    }
}

Commenting out either the log or the await line fixes the issue.

My config:

max_width = 100
hard_tabs = false
tab_spaces = 4
array_width = 80
chain_width = 80
single_line_if_else_max_width = 50
newline_style = "Unix"
edition = "2021"

match_arm_leading_pipes = "Preserve"

Version:

$ rustfmt --version
rustfmt 1.8.0-stable (90b35a6239 2024-11-26)
@ytmimi
Copy link
Contributor

ytmimi commented Jan 8, 2025

The .await makes this a chain. The deeply nested nature of this code is what makes it hard fro rustfmt to deal with. The line containing log::info!("A relatively long string 111111111111111111111111111111111111111111"); is 114 characters long, which exceeds the max width.

If you bump the max_width to some sufficiently high number (I tried 120), then the code reformats:

struct Foo;

impl Foo {
    async fn foo(&mut self) {
        if true {
            async {
                if true {
                    if true {
                        if true {
                            log::info!("A relatively long string 111111111111111111111111111111111111111111");
                        }
                    }
                }
            }
            .await;
        }
    }
}

Given that max_width is a hard constraint for rustfmt I'd recommend refactoring code so that it isn't as deeply nested if you can.

@ytmimi ytmimi closed this as completed Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants