Skip to content

Chained method statements are not formatted if they contain a string that exceeds max_width #6556

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

Open
J-Westin opened this issue May 6, 2025 · 0 comments

Comments

@J-Westin
Copy link

J-Westin commented May 6, 2025

The following example does not get formatted at all when running cargo fmt, and does not report anything on cargo fmt --check. I've set max_width = 50 to make it easier to visualize, but similar behavior can be seen with the default max width.

// max width of 50 characters is here ---------->|
fn _finagle() {
    let _my_string =
        "The quick brown fox jumps over the lazy dog."


             .split(" ")
  .next()

            .unwrap();
}

This seems to be triggered by the fact that the string literal cannot be formatted to fit within max_width, which can be demonstrated by shortening it. Removing "lazy" causes the above example to get formatted to this:

// max width of 50 characters is here ---------->|
fn _finagle() {
    let _my_string =
        "The quick brown fox jumps over the dog."
            .split(" ")
            .next()
            .unwrap();
}

However, I would have expected the original example to format to this:

// max width of 50 characters is here ---------->|
fn _finagle() {
    let _my_string =
        "The quick brown fox jumps over the lazy dog."
            .split(" ")
            .next()
            .unwrap();
}

It seems strange for the formatter to give up on the entire statement and ignore random blank lines and indentation if part of it can't be fit within the line length limit.

I'm using rustfmt 1.8.0-stable (05f9846f89 2025-03-31) with edition = "2024".

@J-Westin J-Westin changed the title Chained method statements are not formatted if it contains a string that exceeds max_width Chained method statements are not formatted if they contain a string that exceeds max_width May 6, 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

1 participant