Fix idempotency issue with raw strings #6696
Open
+16
−1
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.
Fixes #6161
Summary
The issue was in the
CharClasses
iterator's raw string parsing logic (src/comment.rs:1358-1372). When encountering the closing"
of a zero-hash raw string (r"..."
), the code incorrectly setchar_kind
toNormal
before transitioning the state machine toCharClassesStatus::Normal
.This caused the closing quote to be classified as
FullCodeCharKind::Normal
instead ofFullCodeCharKind::InString
. TheLineClasses
iterator downstream depends on accurate character classification to determine string boundaries. Misclassifying the closing quote led to incorrect string boundary detection, which cascaded into wrong indentation calculations during macro formatting.More Details: Why the Bug Only Affects Specific Cases
Example 1: Closing quote
"
at line start (❌ Bug triggers)What happens in
LineClasses
for the line",
:Example 2: Content character
c
at line start (✓ Works despite bug)What happens in
LineClasses
for the linec",
:Reference
rustfmt/src/comment.rs
Lines 1531 to 1558 in 50a49e7
rustfmt/src/utils.rs
Lines 615 to 625 in 50a49e7