Skip to content

rustfmt gives legal length comments a ragged edge in argument lists #7167

Open

Description

@jacobsa

Here is a Rust file with no line longer than 100 bytes, and one line equal to 100 bytes:

fn main() {
    if true {
        if true {
            if true {
                if true {
                    if true {
                        if true {
                            foo(
                                // Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
                                // eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
                                // enim ad minim veniam, quis nostrud exercitation ullamco laboris
                                // nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
                                // reprehenderit in voluptate velit esse cillum dolore eu fugiat
                                // nulla pariatur.
                                1,
                            );
                        }
                    }
                }
            }
        }
    }
}

The comment shouldn't be changed by rustfmt, but it does change it, giving it a very ragged edge:

> cat config.toml
wrap_comments = true
comment_width = 100

> rustfmt +nightly --version
rustfmt 1.11.0-nightly (1d81eb4ad9 2026-10-07)

> rustfmt +nightly --emit=stdout --config-path config.toml foo.rs
fn main() {
    if true {
        if true {
            if true {
                if true {
                    if true {
                        if true {
                            foo(
                                // Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
                                // eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
                                // enim ad minim veniam, quis nostrud exercitation ullamco laboris
                                // nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
                                // in reprehenderit in voluptate
                                // velit esse cillum dolore eu fugiat
                                // nulla pariatur.
                                1,
                            );
                        }
                    }
                }
            }
        }
    }
}

Activity

  1. added
    C-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICE
    on Oct 8, 2026
  2. added
    needs-triageThis issue or PR needs triaging to determine its status. Remove label once sufficiently triaged.
    on Oct 8, 2026
  3. added and removed
    needs-triageThis issue or PR needs triaging to determine its status. Remove label once sufficiently triaged.
    on Oct 8, 2026
  4. matthewhughes934 commented on Oct 9, 2026

    @matthewhughes934
    Contributor

    @rustbot claim

  5. matthewhughes934 commented on Oct 9, 2026

    @matthewhughes934
    Contributor

    The off-by-one issue is because we subtract the length of the trailing ; when formatting a statement here which is true for the last line of the statement (i.e the line that will contain the ;) but not true for every other line if we split the statement over multiple lines. I suspect this is also the cause of e.g. #3016 and probably more of the off-by-one issues.

  6. matthewhughes934 commented on Oct 9, 2026

    @matthewhughes934
    Contributor

    Here's a simpler reproduction:

    fn f() {
        foo(
            // this comment is 99 characters wide! The last word should fit on the following line ----- aa
            // This line is 92 characters wide -------------------------------------------------
            1,
        );
    }

    Format with the same settings as above (with rustfmt --check file.rs)

     fn f() {
         foo(
    -        // this comment is 99 characters wide! The last word should fit on the following line ----- aa
    -        // This line is 92 characters wide -------------------------------------------------
    +        // this comment is 99 characters wide! The last word should fit on the following line -----
    +        // aa This line is 92 characters wide
    +        // -------------------------------------------------
             1,
         );
     }

    But if you manually move the aa to the second line it fits fine an nothing is formattined

    fn f() {
        foo(
            // this comment is 99 characters wide! The last word should fit on the following line -----
            // aa This line is 92 characters wide -------------------------------------------------
            1,
        );
    }
  7. matthewhughes934 commented on Oct 9, 2026

    @matthewhughes934
    Contributor

    Also, separate issue: comment_width is ignored in this case (in the original issue and my example comment_width == 100 == max_width)

    fn f() {
        foo(
            // this line is 43 characters wide!
            1,
        );
        // this line is also 43 characters wide
    }

    Format with comment_width=40 and wrap_comments=true and --check reports:

             // this line is 43 characters wide!
             1,
         );
    -    // this line is also 43 characters wide
    +    // this line is also 43 characters
    +    // wide
     }

    Note the fist comment is untouched

  8. jacobsa commented on Oct 22, 2026

    @jacobsa
    Author

    Thanks for looking, @matthewhughes934. Any chance of getting a fix for those root causes?

  9. matthewhughes934 commented on Oct 22, 2026

    @matthewhughes934
    Contributor

    Thanks for looking, @matthewhughes934. Any chance of getting a fix for those root causes?

    The ragged formatting was addressed with #7170, on main the original code now formats as:

                                     // Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
                                     // eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
                                     // enim ad minim veniam, quis nostrud exercitation ullamco laboris
    -                                // nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
    -                                // reprehenderit in voluptate velit esse cillum dolore eu fugiat
    +                                // nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
    +                                // in reprehenderit in voluptate velit esse cillum dolore eu fugiat
                                     // nulla pariatur.
                                     1,
                                 );

    I haven't had a chance to dive deeper into the off-by-one issue

  10. ytmimi commented on Oct 22, 2026

    @ytmimi
    Contributor

    I'll also note that we just did a sync with the main rust repo so you should see the fix come out in one of the next nightly releases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-commentsArea: commentsC-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICEUO-comment_widthUnstable option: comment_widthUO-wrap_commentsUnstable option: wrap_comments

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions