-
Notifications
You must be signed in to change notification settings - Fork 942
Turn closure bodies from exprs into blocks if there are comments #4396
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
Conversation
My apologies for forgetting to squash commits before uploading to master, it's been a while. |
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.
I think this is really nice, the only thing is that it would not fix the case where the comment is between before a body that is already a block (e.g. || /* foo */ { 1 }
). Maybe that can be considered a different issue.
I thought of a really annoying edge case: let one_liner = || /* ? */ 1; This PR will rewrite it as: let one_liner = || { /* ? */
1
}; I'm pretty sure this is because let one_liner = || {
1
}; and rustfmt/src/formatting/comment.rs Lines 204 to 212 in f7ba2db
An edge case, to be sure, but this whole PR is addressing edge cases, so I'll see what I can figure out. |
@ayazhafiz Personally I'd consider that a separate issue, but I think the fix is quite similar to what I've added in this PR. So I'm going to pass on fixing that for now. |
I have encountered something quite fun. I added this test case: let one_line = || /* one line */ 1; with expected output: let one_line = || { /* one line */
1
}; Because, as I mentioned above, But this fails because rustfmt thinks that the output should be: let one_line = || {
/* one line */
1
}; That's great, that's what I'd prefer to see anyway! So let's change the test case to use that output-- Mismatch at tests/source\issue-4384-2.rs:1:
fn main() {
- let one_line = || {
- /* one line */
+ let one_line = || { /* one line */
1
};
} Um. rustfmt is somehow generating both versions? |
I'm not sure I follow what you're saying here, and I'm unable to reproduce this locally with the latest changes on your branch. The preservation of original line placement with I also wouldn't be too concerned about a minor refactor to add a new param to |
Thanks @ayazhafiz and @calebcartwright, the finer-grained control from using I'm happy with the state of this now, it covers every case I could think of. |
Thanks @Solumin! Could you rebase/address the conflicts? Will take a final look over afterwards |
I don't see any conflicts now. Or at least GitHub says it can be merged. |
I can squash the commits but there's conflicts preventing a rebase. No worries, will look at it later |
I got it, just had to look up the right git incantation. Sorry! |
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.
Thanks @Solumin! We should use the ?
operator instead of unwrapping here to let rustfmt's failed formatting logic apply instead of panicking, but should be good to merge afterwards
Perfect, thank you! |
A closure like this:
should be a block instead of a single expression, as per the fmt-rfcs.
Closes #4384