Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove trailing whitespace from multi-line tuple struct field prefix #5708

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ytmimi
Copy link
Contributor

@ytmimi ytmimi commented Mar 7, 2023

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

@ytmimi
Copy link
Contributor Author

ytmimi commented Mar 7, 2023

Here are the relevant differences between the v1 and the newly added v2 tests:
git diff --no-index tests/target/struct_field_doc_comment.rs tests/target/struct_field_doc_comment_v2.rs

 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,
 );

@ytmimi
Copy link
Contributor Author

ytmimi commented Mar 7, 2023

Just ran the Diff Check Job, and everything looks good ✅

src/items.rs Outdated
Comment on lines 1784 to 1903
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();
}
Copy link
Contributor

@kevinji kevinji Jul 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is more straightforward:

Suggested change
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());
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for helping to simplify the implementation

@calebcartwright
Copy link
Member

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?

@ytmimi
Copy link
Contributor Author

ytmimi commented Aug 13, 2023

I'll have to revisit this. I can take a look to see what's going on in rewrite_struct_field_prefix to see if we can prevent emitting the trailing space. I imagine that change would touch more files, but I also agree that it would be better not to emit the trailing space in the first place.

@ytmimi
Copy link
Contributor Author

ytmimi commented Aug 31, 2023

@calebcartwright check out the latest commit. It removes the trailing whitespace when using Version::Two. The main downside is that we need to make several changes in a lot of different places. Let me know if you think this updated change is better and I'll squash the commits.

// format_visibility doesn't have a trailing space in Version::Two
result.push_str(&visibility);
} else {
result.push_str(visibility.trim());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out that there were other places in the codebase that already dealt with trimming the trailing space coming back from format_visibility.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants