Skip to content

Formatting doesn't work partially with significant nesting and long text line #4415

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
nazar-pc opened this issue Sep 5, 2020 · 1 comment
Labels
bug Panic, non-idempotency, invalid code, etc. duplicate

Comments

@nazar-pc
Copy link

nazar-pc commented Sep 5, 2020

Describe the bug

Formatting doesn't work around match in provided reduced example. Formatting for some other parts of the code around method definition works though. Removing at least one character from text string fixes something and formatting starts working again.

To Reproduce

impl Foo {
    pub fn new() -> Self {
        {
            foo.bar({
                {
                    match 1 { _
                        => {
                            if true
                            {
                                foo!("if you remove one character from this line formatting works   ");
                            }
                        }
                    }
                }
            })
        }
    }
}

Expected behavior

Formatting works on such file.

Meta

  • rustfmt version: rustfmt 1.4.18-stable (8157a3f 2020-07-15), rustfmt 1.4.20-nightly (48f6c32 2020-08-09)
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: rustfmt, cargo fmt, via IDE
@nazar-pc nazar-pc added the bug Panic, non-idempotency, invalid code, etc. label Sep 5, 2020
@calebcartwright
Copy link
Member

Thank you for reaching out @nazar-pc. This is a duplicate (#3863) of a known/existing issues with formatting chains when one or more of the chain elements would exceed max_width when formatted. In these cases, rustfmt leaves the original formatting in place and defers to the developer to properly format, because any formatting rustfmt would apply would violate a rule (max width in this case)

Your snippet above contains a chain due to the foo.bar(... on line 4, and the string arg for foo! exceeds the max_width value of 100. When you remove a char from the string the length drops below the limit and so rustfmt can format.

If you are using rustfmt on nightly, then you can try enabling format_strings = true in your rustfmt config which will allow rustfmt to split the string to stay within the max_width limit. Otherwise you can try splitting the string yourself:

...
                                foo!(
                                    "if you remove one character from this line \
                                     formatting works  ",
                                );

I'm going to go ahead and close this, feel free to track #3863

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Panic, non-idempotency, invalid code, etc. duplicate
Projects
None yet
Development

No branches or pull requests

2 participants