Repository navigation
Description
Summary
The global line length limit in rustfmt is configured with max_width. Outside of that, certain types of expression can be configured with their own widths, so that those expressions can have their own custom aesthetics in terms of horizontal vs vertical layout.
For example, array_width limits the length of array literals independently of max_width. comment_width is another of these properties, which limits comments to 80 characters by default. I'm assuming this is to aid users in keeping their place when reading dense blocks of text.
There was an issue (#6801) with wrap_comments not properly enforcing comment_width when things like bulleted lists were involved. It looks like the fix (#6802) involved making comment_width indent-aware; however, I think this is against the spirit of how custom *_width properties should work. All other *_width properties are not applied with indent taken into account. The properties themselves are about the length of the expression itself, independent of indentation.
It would be weird for array_width = 60 to format [1, 2, 3, 4] as a multi-line literal because the line is indented 52 characters. Similarly, it is weird for comment_width to be applied with indents taken into account.
For example:
Expected behavior
// comment_width = 40
// wrap_comments = true
// This is a comment where there is no
// leading indent. Wrapping happens at
// the 40th column.
fn function() {
for y in 0..height {
for x in 0..width {
if foo {
if bar {
if baz {
if should_frobnicate {
// This is a comment where there is a
// leading indent. Wrapping should
// happen once the comment exceeds 40
// characters.
}
}
}
}
}
}
}Actual behavior
// comment_width = 40
// wrap_comments = true
// This is a comment where there is no
// leading indent. Wrapping happens at
// the 40th column.
fn function() {
for y in 0..height {
for x in 0..width {
if foo {
if bar {
if baz {
if should_frobnicate {
// This is a
// comment where
// there is a
// leading indent.
// Wrapping should
// happen once
// the comment
// exceeds 40
// characters.
}
}
}
}
}
}
}Note: The indented comment reaches column 46, so even the indent-aware wrapping exceeds comment_width.
Note: When the comment starts on a single line, the first format pass also adds an empty // line at the end of the indented comment block. It doesn’t come back once removed.
Configuration
This happens with wrap_comments = true, although the examples above also use comment_width = 40 so shallower nesting can be used to demonstrate the issue.
Meta
rustfmt --version:
rustfmt 1.11.0-nightly (92b5b8c87c 2026-10-29)
Activity
ytmimi commentedon Nov 3, 2026
@csmulhern so essentially the behavior you're asking for is something like:
csmulhern commentedon Nov 5, 2026
Yes, that's right. And my understanding is that's how all these works:
For example, with
array_width = 60, andmax_width = 100, the following highly indented code still leaves the array as a single line, as the array literal is 60 characters (not counting[and]) , even though with indentation it is 100 characters long:ytmimi commentedon Nov 5, 2026
@matthewhughes934 any interest in looking into this and modifying the approach we took for #6802? Are there any issues you see with changing the behavior as mentioned above?