Skip to content

fix: don't leave trailing whitespace before a skipped impl item - #6979

Open
shulaoda wants to merge 1 commit into
rust-lang:mainfrom
shulaoda:07-26-fix_don_t_leave_trailing_whitespace_before_a_skipped_impl_item
Open

fix: don't leave trailing whitespace before a skipped impl item#6979
shulaoda wants to merge 1 commit into
rust-lang:mainfrom
shulaoda:07-26-fix_don_t_leave_trailing_whitespace_before_a_skipped_impl_item

Conversation

@shulaoda

Copy link
Copy Markdown
Contributor

Summary

Closes #5521.

process_missing_code records where a trailing whitespace run starts in SnippetStatus::last_wspace, then drops everything from that point when it reaches the newline. The check for whether to record a new start sat in the else if condition:

} else if c.is_whitespace() && status.last_wspace.is_none() {
    status.last_wspace = Some(i);
} else {
    status.last_wspace = None;
}

The second whitespace character of a run fails that condition and falls into the else branch, which clears the start just recorded. Each whitespace character flips the state, so an even length run ends with no start recorded and survives whole, and an odd length run keeps all but its last character. Only a run of one character was trimmed correctly, which is why the bug looks intermittent.

Inside an impl block this leaves whitespace on the otherwise blank line before a #[rustfmt::skip] item. rustfmt then reports error[internal]: left behind trailing whitespace and stops formatting the file.

Moving the is_none check inside the branch pins last_wspace to the first whitespace character of the run. Only a non-whitespace character clears it, so the whole run is dropped.

Review notes

Whitespace characters on the blank line before the skipped item:

written before after
1 0 0
2 2 0
3 2 0
4 4 0
11 10 0
12 12 0

The skipped item has to be a non-first item of an impl block. The same whitespace in a mod, in a function body, at the top level, or before the first impl item is trimmed elsewhere and was never affected, so the test uses that one shape for all four cases.

#4706 was raised on the issue as a possible duplicate. There the whitespace is inside the body of the skipped item, which the attribute asks rustfmt to leave alone. Here it sits between the previous item and the attribute, outside any skipped span.

@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Jul 25, 2026
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.

error[internal]: left behind trailing whitespace

2 participants