Repository navigation
Open
Labels
A-commentsArea: commentsArea: commentsC-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICECategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICEUO-comment_widthUnstable option: comment_widthUnstable option: comment_widthUO-wrap_commentsUnstable option: wrap_commentsUnstable option: wrap_comments
Description
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,
);
}
}
}
}
}
}
}Metadata
Metadata
Assignees
Labels
A-commentsArea: commentsArea: commentsC-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICECategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICEUO-comment_widthUnstable option: comment_widthUnstable option: comment_widthUO-wrap_commentsUnstable option: wrap_commentsUnstable option: wrap_comments
Activity
matthewhughes934 commentedon Oct 9, 2026
@rustbot claim
matthewhughes934 commentedon Oct 9, 2026
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.matthewhughes934 commentedon Oct 9, 2026
Here's a simpler reproduction:
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
aato the second line it fits fine an nothing is formattinedmatthewhughes934 commentedon Oct 9, 2026
Also, separate issue:
comment_widthis ignored in this case (in the original issue and my examplecomment_width == 100 == max_width)Format with
comment_width=40andwrap_comments=trueand--checkreports:// 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
Fix comments not taking full width when combined
Fix comments not taking full width when combined
Enforce comment width in item lists
jacobsa commentedon Oct 22, 2026
Thanks for looking, @matthewhughes934. Any chance of getting a fix for those root causes?
matthewhughes934 commentedon Oct 22, 2026
The ragged formatting was addressed with #7170, on
mainthe 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
ytmimi commentedon Oct 22, 2026
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