Fix fatal error caused by switch cases without any statements (#473) #499
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The issue can be reproduced on main by formatting the following snippet:
The code isn't valid Swift of course, however it shouldn't cause a fatal error, and in my opinion it shouldn't prevent formatting of the code (because intent is clear). However, I'm not sure of the official position of this project on how invalid code should be handled.
Note that adding a comment in the
y
case doesn't effect whether a fatal error occurs.I have added a test that covers formatting of empty cases and after my relatively minimal (and commented) changes it no longer crashes. The issue occurred because in the situation where a case is empty, the opening break and
open
token are added after the same syntax token as the closing break andclose
token. These two sets of tokens are added one after another, and because of the implementation ofafter
, the two sets end up getting swapped when constructing the token stream. Thus, the closing break andclose
token end up preceding the opening break andopen
token causing the fatal error.