-
Notifications
You must be signed in to change notification settings - Fork 931
Fixed comment formatting issue between assignment operator and rhs #4440
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
Fixed comment formatting issue between assignment operator and rhs #4440
Conversation
Thanks so much for the PR @whizsid! I've only had a chance to do a quick glance through this, will try to set aside some time in the next couple days to go through in detail. One initial thought I had is that I still think it would be good if we could have a more generic/reusable approach for handling assignments that have comments between the lhs and rhs. The existing util functions are pretty clear that they expect lhs to (inclusively) end with the assignment operator, but it feels like we keep solving these situations by peeking into the rhs and attaching the comments between the assignment op and the rhs to the lhs because these util functions are dropping those comments. This has almost certainly resulted in some duplicated efforts throughout the codebase. What if we created a new function (something along the lines of The logic conditions would be basically the same as what I gathered you were doing here, though do keep in mind that it's preferable if rustfmt can preserve comment-associated line breaks so const B: usize =
/* Some constant */
3; would ideally be formatted as const B: usize =
/* Some constant */
3; while const C : usize
= /* foo */5; would be: const C: usize = /* foo */ 5; (this is where Think we should be able to achieve that pretty easily solely within this new function by:
Then the only other change needed to fix the static item bug would be to switch the call from What do you think? |
@calebcartwright It is a really good idea to implement a new function. I faced a problem when implementing it. |
Could you elaborate on what you mean? There's quite a lot of trimming done within rustfmt, both start and end, in various places to ensure proper formatting and alignment, so I'd push back against calling the usage of a trim function as non-idiomatic. That being said, ideally we wouldn't prepend the leading space on the rhs unnecessarily in the first place only to peel it off later (though again there's quite a few instances of this type of workflow within rustfmt). It'd take quite a bit of additional refactoring to be able to achieve that goal though, so I'd prefer to defer that to future PRs. |
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.
Awesome, thank you @whizsid. The approach definitely seems like it'll work and the PR is in pretty good shape! Couple minor inline changes requested along with a few additional test scenarios, but should be good to merge after those are addressed
@calebcartwright I have changed the code as you guided. Can you review it again? |
Awesome, thanks so much! |
Looks like this was backported in #4632. |
Fixes #4427 .
I tried to use
combine_strs_with_missing_comments
inrewrite_assign_rhs_with
. But it is a huge change and already fixed it in some places using boiler plates. So I didn't take that approach.