Skip to content

Commit b72ad8f

Browse files
github-actions[bot]phpstan-bot
authored andcommitted
Fix phpstan/phpstan#11181: Allow bare yield after return for empty generators
- 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
1 parent c36922b commit b72ad8f

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

src/Analyser/NodeScopeResolver.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4442,6 +4442,14 @@ private function getNextUnreachableStatements(array $nodes, bool $earlyBinding):
44424442
if ($node instanceof Node\Stmt\Nop || $node instanceof Node\Stmt\InlineHTML) {
44434443
continue;
44444444
}
4445+
if (
4446+
$node instanceof Node\Stmt\Expression
4447+
&& $node->expr instanceof Expr\Yield_
4448+
&& $node->expr->key === null
4449+
&& $node->expr->value === null
4450+
) {
4451+
continue;
4452+
}
44454453
if (!$node instanceof Node\Stmt) {
44464454
continue;
44474455
}

tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,12 @@ public function testBug14328(): void
397397
]);
398398
}
399399

400+
public function testBug11181(): void
401+
{
402+
$this->treatPhpDocTypesAsCertain = true;
403+
$this->analyse([__DIR__ . '/data/bug-11181.php'], []);
404+
}
405+
400406
public function testBug14369(): void
401407
{
402408
$this->treatPhpDocTypesAsCertain = true;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Bug11181;
4+
5+
function foo(): \Iterator
6+
{
7+
return;
8+
yield;
9+
}
10+
11+
function bar(): \Generator
12+
{
13+
return;
14+
yield;
15+
}
16+
17+
function baz(): iterable
18+
{
19+
return;
20+
yield;
21+
}

0 commit comments

Comments
 (0)