Skip to content

Fix comments rewritten too long#6802

Open
matthewhughes934 wants to merge 2 commits intorust-lang:mainfrom
matthewhughes934:fix-comments-rewritten-too-long
Open

Fix comments rewritten too long#6802
matthewhughes934 wants to merge 2 commits intorust-lang:mainfrom
matthewhughes934:fix-comments-rewritten-too-long

Conversation

@matthewhughes934
Copy link

@matthewhughes934 matthewhughes934 commented Feb 13, 2026

  • Simplify test case for issue 5023

    By replacing the code quoted from the issue with a single comment that
    more directly exposes the issue. This is to

    1. Make the underlying issue easier to reason about
    2. Make this test not behave upon the behaviour of indentation length
      when rewriting lines (my main motivation for this change, since it
      makes the followup patch simpler)

    This was tested by partially reverting
    368a9b7 with the patch:

    diff --git i/src/string.rs w/src/string.rs
    index 3b971188..561e70ac 100644
    --- i/src/string.rs
    +++ w/src/string.rs
    @@ -278,9 +278,6 @@ fn break_string(max_width: usize, trim_end: bool, line_end: &str, input: &[&str]
             }
             cur_index
         };
    -    if max_width_index_in_input == 0 {
    -        return SnippetState::EndOfInput(input.concat());
    -    }
    
         // Find the position in input for breaking the string
         if line_end.is_empty()
    

    The re-running tests, or executing rustmft directly on this test,
    triggered the panic from the original bug.

  • Fix wrapping of comments sometimes making too long lines

    In the case where comment_width is less than max_width and we want
    to wrap right on comment_width: rustfmt would consider it's wrapping
    length to be exactly comment_width, ignoring the length of a trailing
    indent. The fix is essentially how similar formatting code behaves, see
    e.g. shape::Shape::comment.

    This involved rewriting some existing tests, without impacting what
    they're meant to be asserting on. This emphasises that this patch is
    not
    backwards compatible: while I believe the change to be correct
    (some tests had comments longer than comment_width that weren't being
    formatted). So there's a trade-off to be made between correctness and
    stability, since comment_width is still not stabilised this may be
    acceptable.

    Issue: wrap_comments partially ignores comment_width with bullet lists #6801

@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Feb 13, 2026
By replacing the code quoted from the issue with a single comment that
more directly exposes the issue. This is to

1. Make the underlying issue easier to reason about
2. Make this test not behave upon the behaviour of indentation length
   when rewriting lines (my main motivation for this change, since it
   makes the followup patch simpler)

This was tested by partially reverting
368a9b7 with the patch:

  diff --git i/src/string.rs w/src/string.rs
  index 3b97118..561e70ac 100644
  --- i/src/string.rs
  +++ w/src/string.rs
  @@ -278,9 +278,6 @@ fn break_string(max_width: usize, trim_end: bool, line_end: &str, input: &[&str]
           }
           cur_index
       };
  -    if max_width_index_in_input == 0 {
  -        return SnippetState::EndOfInput(input.concat());
  -    }

       // Find the position in input for breaking the string
       if line_end.is_empty()

The re-running tests, or executing `rustmft` directly on this test,
triggered the panic from the original bug.
In the case where `comment_width` is less than `max_width` and we want
to wrap right on `comment_width`: `rustfmt` would consider it's wrapping
length to be exactly `comment_width`, ignoring the length of a trailing
indent. The fix is essentially how similar formatting code behaves, see
e.g. `shape::Shape::comment`.

This involved rewriting some existing tests, without impacting what
they're meant to be asserting on. This emphasises that this patch _is
not_ backwards compatible: while I believe the change to be correct
(some tests had comments longer than `comment_width` that weren't being
formatted). So there's a trade-off to be made between correctness and
stability, since `comment_width` is still not stabilised this may be
acceptable.

Issue: rust-lang#6801
@matthewhughes934 matthewhughes934 force-pushed the fix-comments-rewritten-too-long branch from cecee09 to 9b9090f Compare February 13, 2026 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants