comment_width should be independent of indent

オープン
#7,230 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rust
領域
tooling

調査の方向性

The issue asks that comment_width be measured without the indentation of the comment, and it points to #6802 as the change that made it indent-aware. Start by reading that pull request to see where the indent is subtracted, then reproduce the two examples with comment_width = 40 and wrap_comments = true. Done means the indented comment wraps at 40 characters of its own text, the empty trailing // line is not added, and the existing wrap_comments tests still pass. Because the maintainers may need to agree on the design first, check for a decision before you start.

索引モデルが issue の本文から書いたものです。

説明

C-bug UO-comment_width UO-wrap_comments

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)
主要言語
Rust
スター
7.1k
フォーク
1.2k
平均マージ
1日 4時間
マージ済み PR(30日)
20

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

rust-lang/rustfmt のほかの issue

rust-lang/rustfmt の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。