Fix rustfmt giving up on formatting when multi-line strings exceed max_width #6781
+169
−11
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 becausefiltered_str_fits()insrc/utils.rswould 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 tuplestests/source/issue-6778.rs/tests/target/issue-6778.rs- multi-line strings in struct fieldsReferences
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.rsin a not so desirable way: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.