Skip to content

Fix phpstan/phpstan#11181: Empty generator "return; yield;" leads to unreachable statement error#5354

Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-8u7m9gq
Open

Fix phpstan/phpstan#11181: Empty generator "return; yield;" leads to unreachable statement error#5354
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-8u7m9gq

Conversation

@phpstan-bot
Copy link
Copy Markdown
Collaborator

Summary

PHP allows return; yield; as an idiom to create an empty generator — the yield makes PHP treat the function as a generator even though it never executes. PHPStan was incorrectly reporting the yield; as an unreachable statement.

Changes

  • Modified src/Analyser/NodeScopeResolver.php: In getNextUnreachableStatements(), skip Expression nodes containing a bare Yield_ (no key, no value) so they are not flagged as unreachable
  • Added tests/PHPStan/Rules/DeadCode/data/bug-11181.php with test cases for Iterator, Generator, and iterable return types
  • Added testBug11181() in tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php

Root cause

getNextUnreachableStatements() already skips certain benign statements after terminating code (Nop, InlineHTML, early-bound declarations). It did not account for bare yield; statements, which serve as generator markers in PHP and are a well-known idiom for creating empty generators.

Test

The regression test covers three variants of the return; yield; pattern with different return type hints (Iterator, Generator, iterable). All should analyse without errors.

Fixes phpstan/phpstan#11181

…nerators

- Skip bare `yield;` (no key, no value) in getNextUnreachableStatements
- This is a valid PHP idiom (`return; yield;`) to create an empty generator
- Added regression test in tests/PHPStan/Rules/DeadCode/data/bug-11181.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant