Repository navigation
Treat a switch whose cases cover every value as exhaustive - #7264
Open
ondrejmirtes wants to merge 3 commits into
Open
ondrejmirtes wants to merge 3 commits into
ondrejmirtes wants to merge 3 commits into
Conversation
A case whose `subject == case` comparison is always true leaves no value for the code below the switch, like an always-true elseif. The subject is now narrowed by every case that did not match, also across cases falling through into each other, the same way match narrows after each arm condition. An exhaustive switch no longer reports return.missing, the code after it is unreachable, and an always-true last case followed by a default case is reported like the corresponding match arm. Amp-Thread-ID: https://ampcode.com/threads/T-01a22b99-13c4-7062-a0a0-1c86517418dc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
A
switchwas exhaustive only when the subject's remaining type becamenever. Inswitch (true)the subject staystrue, so a case that always matches never made the switch exhaustive, unlike the equivalentif/elseifchain.subject == casecomparison is always true now makes the switch exhaustive: noreturn.missing, and code after the switch is unreachable.matchnarrows after each arm condition. Previously it was narrowed only after a terminating group, through a growingBooleanOr. The always-true verdict is composed from the subject's and the case's results throughresolveEqualType(), with no on-demand walk.matcharm, an always-true last case is not reported unless adefaultcase it makes unreachable follows it (switch.alwaysTrue, bleeding edge).SwitchHandlertwin is updated accordingly.Closes phpstan/phpstan#15892