-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Quick assist to convert switch statements to switch expressions #50417
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
The question, in my mind, is whether this should be tied to a 'prefer_switch_expressions' lint, or whether this should just be available as a quick assist in places where it could be applied. (I'd be hesitant to say that all such switch statements are marked as a problem without users explicitly choosing that style.) |
If it were me, I wouldn't make this a "prefer" lint. Just an assist users can manually invoke for specific switch statements if they want to. I agree with you that I don't think all switch statements should be expressions just because they could be. Sometimes a statement is just easier to read. |
We probably shouldn't convert a statement that isn't exhaustive, so we'll need to wait for that piece to be done before we implement the assist. |
Proposal here: #58862 |
See: #50417 Initial work to support return conversions. Arguments and assignments to come and will likely lead to some refactoring but there's enough here to benefit from some early feedback. Thanks in advance! :D Change-Id: Ic3d0349aa12d8c951e3afe0da3e00e2777480e38 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/286861 Commit-Queue: Phil Quitslund <pquitslund@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Implemented w/ b68719c |
See also #51826 that adds support for case clauses w/ implicit breaks. |
With the in-progress work on patterns, we're also adding a new switch expression. It would be very cool if we could support a quick fix that would allow you to convert switch statements to switch expressions when it makes sense. I think that's feasible when:
return
statement, orThen in that case, the shared structure in the cases can be hoisted out and the switch statement turned into a switch expression whose result is then applied to the shared structure. So, for example:
The text was updated successfully, but these errors were encountered: