Skip to content

Conversation

@thomasqueirozb
Copy link

@thomasqueirozb thomasqueirozb commented Jan 26, 2026

Summary

Fix rustfmt giving up on formatting when multi-line strings exceed max_width.

rustfmt would refuse to format code containing multi-line string literals that exceed max_width. The problem occurred because filtered_str_fits() in src/utils.rs would reject valid formatting attempts when string content (which cannot be shortened) exceeded width limits.

Added test fixtures for both issues:

  • tests/source/issue-6769.rs / tests/target/issue-6769.rs - byte string literals in array tuples
  • tests/source/issue-6778.rs / tests/target/issue-6778.rs - multi-line strings in struct fields

References


Note that reverting commit fa5b1e4 "breaks" a bunch of fixtures for the better, allowing formatting of rust code even after very long strings.

Example:

Mismatch at tests/source/string_punctuation.rs:5:
         "ThisIsAReallyLongStringWithNoSpaces.It_should_prefer_to_break_onpunctuation:\
          Likethisssssssssssss"
     );
-    format!("{}__{}__{}ItShouldOnlyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyNoticeSemicolonsPeriodsColonsAndCommasAndResortToMid-CharBreaksAfterPunctuation{}{}",x,y,z,a,b);
+    format!(
+        "{}__{}__{}ItShouldOnlyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyNoticeSemicolonsPeriodsColonsAndCommasAndResortToMid-CharBreaksAfterPunctuation{}{}",
+        x, y, z, a, b
+    );

and

Mismatch at tests/source/issue-2179/one.rs:27:
         } else {
             for package in &opts.package {
                 if let None = ws.members().find(|x| x.name() == package) {
-                    warn!("cargo - couldn't find member package `{}` specified in `analyze_package` configuration", package);
+                    warn!(
+                        "cargo - couldn't find member package `{}` specified in `analyze_package` configuration",
+                        package
+                    );
                 }
             }
         }

It does, however, break tests/source/issue-1210/c.rs in a not so desirable way:

Mismatch at tests/source/issue-1210/c.rs:1:
 // rustfmt-format_strings: true
 // rustfmt-max_width: 50

-const foo: String =
-    "trailing_spaces!!
+const foo: String = "trailing_spaces!!
                 keep them! Amet neque. Praesent \
-     rhoncus eros non velit.";
+                     rhoncus eros non velit.";

I'd encourage someone to take a look at this and follow up if possible.


Note: This PR was generated with assistance from an LLM (Claude). While I'm happy to address minor feedback, I may not have bandwidth to drive major revisions or extensive back-and-forth due to time constraints. If this approach looks promising but needs significant changes, feel free to take over the branch or use this as a starting point. I mainly wanted to get a potential fix out there since these issues were affecting my workflow but since the issue was actually fixed I thought it was worth sharing these changes.

@rustbot rustbot added the S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. label Jan 26, 2026
@thomasqueirozb thomasqueirozb marked this pull request as ready for review January 26, 2026 20:00
@rustbot rustbot added S-waiting-on-review Status: awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. labels Jan 26, 2026
@ytmimi
Copy link
Contributor

ytmimi commented Jan 26, 2026

Just ran the Diff-Check, and its already started finding breaking formatting changes. There's no way we can move forward with the PR as is.

@ytmimi
Copy link
Contributor

ytmimi commented Jan 26, 2026

These changes would need to be style_edition gated, but even then I'm not sure if the formatting changes that they introduce are worth it.

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: awaiting review from the assignee but also interested parties. labels Jan 26, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 26, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@thomasqueirozb
Copy link
Author

thomasqueirozb commented Jan 26, 2026

This is unfortunate. All checks passed locally so I assumed this was fine but I wasn't aware of diff-check. Thanks for looking into this. I think I'll close this PR then, but someone can probably use this as a starting point.

@rustbot rustbot removed the S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. label Jan 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: rustfmt silently breaks on this sample rustfmt gives up if second part of multiline string is >= 100 chars long

3 participants