remove trailing whitespace from multi-line tuple struct field prefix#5708
remove trailing whitespace from multi-line tuple struct field prefix#5708ytmimi wants to merge 3 commits into
Conversation
|
Here are the relevant differences between the struct MyTuple(
#[cfg(unix)] // some comment
- pub u64,
- #[cfg(not(unix))] /*block comment */ pub(crate) u32,
+ pub u64,
+ #[cfg(not(unix))] /*block comment */ pub(crate) u32,
);
struct MyTuple(
/// Doc Comments
/* TODO note to add more to Doc Comments */
- pub u32,
+ pub u32,
/// Doc Comments
// TODO note
- pub(crate) u64,
+ pub(crate) u64,
); |
|
Just ran the Diff Check Job, and everything looks good ✅ |
| while context.config.version() == Version::Two && prefix.ends_with(char::is_whitespace) { | ||
| // Remove any additional whitespace at the end of the prefix. | ||
| // For example if there is a space after a visibility modifier. | ||
| prefix.pop(); | ||
| } |
There was a problem hiding this comment.
I think this is more straightforward:
| while context.config.version() == Version::Two && prefix.ends_with(char::is_whitespace) { | |
| // Remove any additional whitespace at the end of the prefix. | |
| // For example if there is a space after a visibility modifier. | |
| prefix.pop(); | |
| } | |
| if context.config.version() == Version::Two { | |
| // Remove trailing whitespace after the prefix, such as a visibility modifier. | |
| prefix.truncate(prefix.trim_end().len()); | |
| } |
There was a problem hiding this comment.
Thanks for helping to simplify the implementation
|
I'd be content merging this as-is because it cleanly fixes an obvious and annoying issue (and I'll add I hate we have to gate this, but agree that we unfortunately must). However, it's typically a smell for me when we have to remove whitespace that we previously added farther back in the call stack. Do you think it would be feasible to apply a different fix that only adds the whitespace conditionally when necessary? And if so, is that something you think we could turn around quickly or would it be better to move forward with this fix and add a tracking issue and/or fixme comment to try that alternative approach? |
|
I'll have to revisit this. I can take a look to see what's going on in |
|
@calebcartwright check out the latest commit. It removes the trailing whitespace when using |
| // format_visibility doesn't have a trailing space in Version::Two | ||
| result.push_str(&visibility); | ||
| } else { | ||
| result.push_str(visibility.trim()); |
There was a problem hiding this comment.
It turns out that there were other places in the codebase that already dealt with trimming the trailing space coming back from format_visibility.
There was a problem hiding this comment.
Hm, it feels like a footgun that format_visibility also attaches trailing whitespaces.
Fixes 5703, Fixes 5525 visibility modifiers always contain a trailing space after them. If the formatted tuple field needs to be written over multiple lines then the extra space will cause issues. In the best case the space will offset the type name by an extra space and in the worst case it will lead to a "left behind trailing whitespace" error.
There was a problem hiding this comment.
(Excuse the review 3 years later 🦆)
I think the approach taken in this PR is reasonable enough for a fix that IMO is worth landing. A separate follow-up investigation on the handling of format_visibility is likely warranted though, that it attaches a trailing space I agree feels quite sus.
(Though this PR will need a... rebase)
@rustbot author
| @@ -0,0 +1,9 @@ | |||
| // rustfmt-version: Two | |||
There was a problem hiding this comment.
Suggestion: // rustfmt-style_edition: 2027
(Since style edition 2024 is already stable?)
Likewise in other tests.
| if !vis.is_empty() && context.config.version() == Version::Two { | ||
| vis += " "; | ||
| } |
There was a problem hiding this comment.
Suggestion: && context.config.style_edition() >= StyleEdition::Edition2027 I think
| if !vis.is_empty() && context.config.version() == Version::Two { | ||
| // format_visibility doesn't have a trailing space in Version::Two | ||
| result.push(' '); | ||
| } |
There was a problem hiding this comment.
Suggestion:
!vis.is_empty() && context.config.version() == Version::Two->!vis.is_empty() && context.config.style_edition() >= StyleEdition::2027// format_visibility doesn't have a trailing space in Version::Two->// format_visibility doesn't have a trailing space in StyleEdition::2027
and elsewhere
| // format_visibility doesn't have a trailing space in Version::Two | ||
| result.push_str(&visibility); | ||
| } else { | ||
| result.push_str(visibility.trim()); |
There was a problem hiding this comment.
Hm, it feels like a footgun that format_visibility also attaches trailing whitespaces.
|
Reminder, once the PR becomes ready for a review, use |
|
☔ The latest upstream changes (possibly #6823) made this pull request unmergeable. Please resolve the merge conflicts. |
Fixes #5703, Fixes #5525, Fixes #5997
visibility modifiers always contain a trailing space after them. If the formatted tuple field needs to be written over multiple lines then the extra space will cause issues.
In the best case the space will offset the type name by an extra space and in the worst case it will lead to a
"left behind trailing whitespace"error.These changes were version gated because they cause breaking formatting changes with existing tests.
r? @calebcartwright