Skip to content

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

Closed as duplicate of#3863
@J-Westin

Description

@J-Westin

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".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions