-
Notifications
You must be signed in to change notification settings - Fork 912
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
Fix Comment Preservation in PatKind::Paren Patterns #6111
base: master
Are you sure you want to change the base?
Conversation
@yuvraj-wale unfortunately we can't accept these changes as is since they modify the formatting of already formatted code. rustfmt has strong guarantees about not introducing stable formatting changes so you'll likely need to come up with a different approach to solve this one. One idea you could try is to manually write the comment recovery code. We might be able to move forward with these changes if they were version gated, though I'd want some clarify from the style team on whether or not the new formatting is what's expected based on the Rust Style Guide |
Thanks for the feedback! I will look into recovering comments manually and try to update the PR. |
Great. Let me know if you need any pointers on how to get started |
Hey @ytmimi, |
I'm wondering if Another approach to preserve comments within the |
@ytmimi |
@yuvraj-wale possibly. I think you'll need to add test cases to this PR to know for sure. |
let pre_snippet = context.snippet(mk_sp(self.span.lo(), pat.span.lo())); | ||
let post_snippet = context.snippet(mk_sp(pat.span.hi(), self.span.hi())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I was suggesting we return context.snippet(span)
I was thinking more along the lines of the following approach where we don't apply any formatting, not even to the inner pattern.
let pre_snippet = context.snippet(self.span.lo().between(pat.span.lo()));
let post_snippet = context.snippet(pat.span.lo().between(self.span.hi()));
if contains_comment(pre_snippet) || contains_comment(post_snippet) {
return Some(context.snippet(self.span).to_owned())
}
Your approach may also work though! As I mentioned I'd like you to add some test cases to better prove this out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey I've added some test cases, let me know if there's anything specific you'd like to see.
bb76851
to
109c6b7
Compare
109c6b7
to
be1585c
Compare
Fixes #6110
Description:
This pull request resolves an issue where rustfmt was removing comments inside parentheses in PatKind::Paren patterns. The fix ensures that comments are preserved during formatting, maintaining the original code structure and readability.
Changes: