Skip to content

comment_width should be independent of indent #7230

Open

Description

@csmulhern

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

  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 Nov 3, 2026
  2. added
    needs-triageThis issue or PR needs triaging to determine its status. Remove label once sufficiently triaged.
    on Nov 3, 2026
  3. ytmimi commented on Nov 3, 2026

    @ytmimi
    Contributor

    @csmulhern so essentially the behavior you're asking for is something like:

    allowed_comment_width = min(configured_comment_width, available_width_on_the_line)
    
  4. removed
    needs-triageThis issue or PR needs triaging to determine its status. Remove label once sufficiently triaged.
    on Nov 5, 2026
  5. csmulhern commented on Nov 5, 2026

    @csmulhern
    ContributorAuthor

    Yes, that's right. And my understanding is that's how all these works:

    array_width
    attr_fn_like_width
    chain_width
    fn_call_width
    single_line_if_else_max_width
    single_line_let_else_max_width
    struct_lit_width
    struct_variant_width
    

    For example, with array_width = 60, and max_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:

    // array_width = 60
    // max_width = 100
    
    fn function() {
        for y in 0..height {
            for x in 0..width {
                if foo {
                    if bar {
                        if should_frobnicate {
                            // There are 60 characters between [ and ]. Rustfmt does not reflow this.
                            let values = [first_element, second_element, third_element, fourth_element];
                        }
                    }
                }
            }
        }
    }
  6. ytmimi commented on Nov 5, 2026

    @ytmimi
    Contributor

    @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?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-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