Fix phpstan/phpstan#9444: return statement is missing#5307
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#9444: return statement is missing#5307phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- Added handling for Goto_ in NodeScopeResolver to mark it as always terminating, since goto transfers control and makes subsequent code unreachable - New regression test in tests/PHPStan/Rules/Missing/data/bug-9444.php covering goto-based retry patterns with try/return/catch - The root cause was that PHPStan had no handling for goto statements, so code after goto was considered reachable, causing false positive "return statement is missing" errors in goto-based retry patterns
Contributor
|
I'm not sure fixing Also I wonder if this "quick fix" won't trigger some other false positive like reporting unreachable code. This will need some tests. |
Member
|
goto needs similar handling as loops have (multiple iterations, generalizing scope) |
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.
Summary
PHPStan reported false positive "return statement is missing" errors for goto-based retry patterns where a
returninside atryblock was followed by acatchthat usedgototo retry. The fix treatsgotoas an always-terminating statement since it transfers control flow, making code after it unreachable.Changes
Goto_handling insrc/Analyser/NodeScopeResolver.php(around line 910) to returnisAlwaysTerminating = truewith no exit pointstests/PHPStan/Rules/Missing/data/bug-9444.phpwith three goto-based retry patternstestBug9444intests/PHPStan/Rules/Missing/MissingReturnRuleTest.phpRoot cause
PHPStan's
NodeScopeResolverhad no handling forGoto_statements at all. When processing statements sequentially, agotowas effectively treated as a no-op, meaning code aftergotowas considered reachable. This caused false positives in patterns like:The catch block's
ifstatement was not recognized as always-terminating because thegotobranch wasn't terminating. This propagated up: the catch wasn't always-terminating, so the try/catch wasn't, so the function body wasn't, triggering the missing return error.The fix is minimal: treat
gotoas always-terminating (likereturn,break,continue) since it unconditionally transfers control to a label, making any code after it unreachable.Test
Added
tests/PHPStan/Rules/Missing/data/bug-9444.phpwith three test cases:gotoRetry()- goto in catch with conditional retry and throw on exhaustiongotoRetrySimple()- unconditional goto retry in catchgotoInCatch()- goto in catch where code after try/catch is reachable (verifies no false negatives)All expect zero
return.missingerrors.Fixes phpstan/phpstan#9444