-
Notifications
You must be signed in to change notification settings - Fork 898
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
Implement let-else
formatting
#5690
Conversation
I think the PR is good to go, but I plan to add some additional test cases with comments between the initializer expression and the |
This should not have an impact given the current applicable domains, as I recognize that there will likely be some non-zero portion of the user base that would like to force the brace onto the next line, but I think that needs to be considered against the more holistic lines of the posture outlined in #3376 (comment); I don't want to try to shoehorn yet another AST context into the existing config options |
@calebcartwright, given the way I implemented fn main() {
let Some(x) = opt else {
return;
};
} with fn main() {
let Some(x) = opt
else {
return;
};
} or with fn main() {
let Some(x) = opt
else
{
return;
};
} I implemented the |
To clarify, I was speaking about expected formatting behavior, and not current/actual behavior of the proposed implementation. I recognize that there's a... control flow aspect to this given the divergent nature of the else block (certainly more so than other types of statement, item, etc. contexts), but I'm still iffy on whether or not we should tie |
Hi. Is there anything we outsiders can help with to get this moving forward? |
03adba6
to
c0d899d
Compare
@fgsch thanks for asking! I always appreciate feedback, so if anyone has thoughts on simplifying the implementation or adding more clarity to the code please let me know. Additionally, if anyone has ideas for test cases that haven't already been covered that would be a great help. |
Echo the appreciation. My assumption was that it was more of a bump to gauge status, so also want to note that this is one we'd discussed offline in chat/team meetings. I owned the action item to recap those discussions and remaining elements back here on the PR and obviously failed there, so apologies for the lack of transparency. I do think we're just about sorted implementation wise, but also want to note that there will be at least one more implementation PR and then some release/communication work to follow before this actually gets into user's hands. |
let Some(x) = opt else { | ||
// nope | ||
return; | ||
}; |
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 there's a missing case of
let Some(x) = opt else {
return
}
Also I don't see any examples with long patterns after let
. Probably good to add variation to the types of patterns in general.
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.
In particular, struct patterns, tuples and arrays can raise some unique formatting concerns.
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 there's a missing case of
let Some(x) = opt else { return }
I can add a test case for that!
Also I don't see any examples with long patterns after let
Now that you bring it up I should probably add one or two to make sure that we wrap the else block
onto its own line if the pattern is multi-lined.
c0d899d
to
a80e63a
Compare
d359679
to
4e1f14e
Compare
4e1f14e
to
6dfd0e9
Compare
Another quick bump here :) Please let us outsiders know if we can be of help. |
Appreciate the offer, but there's nothing needed from the community nor anything the community can do to help (unless you happen to review the changes and think you've spotted an issue). Similarly, there's no need for any continued bumps on the thread. This is not forgotten, this is currently our top priority. However, as I alluded to in #5690 (comment), there's a lot of moving parts required to get to a point where let-else support will be available in released versions of rustfmt, the majority of which are unrelated to this particular PR and which are not going to be reflected here. It's intentional that we've not merged this PR yet, and this PR being merged vs. unmerged does not accurately portray status. |
@ytmimi we should be all set now on being able to merge, so if you could rebase whenever time permits that'd be most appreciated |
The function properly handles recovering comments before and after the `else` keyword, and properly handles how to write the else when users configure `control_brace_style`.
This will make it easier to format the divergent blocks of `let-else` statements since it'll be easier to prevent the block from being formatted on a single line if the preconditions aren't met.
`rewrite_let_else_block` gives us more control over allowing the `else` block to be formatted on a single line than `<ast::Block as Rewrite>::rewrite`.
These test cases try to cover various edge cases. For example, comments around the else keyword and long, unbreakable, single-line initializer expressions, and long patterns.
This helps to prevent max width errors.
6dfd0e9
to
5fc3e93
Compare
let Some(x) = opt else { | ||
// nope | ||
return; | ||
}; |
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.
as described here, this should have been
let Some(x) = opt else {
// nope
return
};
without ;
after return
to make sure that a single expression is not formatted on a single line if it contains a comment
Rustfmt version 1.6.0 released on 2023-07-02 added support for let-else statement (PR rust-lang/rustfmt#5690). Make the code compliant.
Rustfmt version 1.6.0 released on 2023-07-02 added support for let-else statement (PR rust-lang/rustfmt#5690). Make the code compliant.
Formatting for this kind of statements was added by rust-lang/rustfmt#5690. We had statements prior to this being available, so we should now fix those.
ref #4914
r? @calebcartwright
Implements
let-else
formatting based on the rules outlined in rust-lang/rust#107312. The examples listed in the style guide are included as simple test cases to validatelet-else
formatting.Note This PR does not implement a "short" config for
let-else
.