Skip to content

Commit 056c434

Browse files
authored
Fix missing detection of dead code in arrow functions
1 parent 8da8b2d commit 056c434

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3192,7 +3192,7 @@ static function (): void {
31923192
return new ExpressionResult(
31933193
$result->getScope(),
31943194
$result->hasYield(),
3195-
$result->isAlwaysTerminating(),
3195+
false,
31963196
[],
31973197
[],
31983198
);
@@ -4804,7 +4804,7 @@ private function processArrowFunctionNode(
48044804
$nodeCallback(new InArrowFunctionNode($arrowFunctionType, $expr), $arrowFunctionScope);
48054805
$exprResult = $this->processExprNode($stmt, $expr->expr, $arrowFunctionScope, $nodeCallback, ExpressionContext::createTopLevel());
48064806

4807-
return new ExpressionResult($scope, false, false, $exprResult->getThrowPoints(), $exprResult->getImpurePoints());
4807+
return new ExpressionResult($scope, false, $exprResult->isAlwaysTerminating(), $exprResult->getThrowPoints(), $exprResult->getImpurePoints());
48084808
}
48094809

48104810
/**
@@ -5232,6 +5232,7 @@ private function processArgs(
52325232
if ($callCallbackImmediately) {
52335233
$throwPoints = array_merge($throwPoints, array_map(static fn (ThrowPoint $throwPoint) => $throwPoint->isExplicit() ? ThrowPoint::createExplicit($scope, $throwPoint->getType(), $arg->value, $throwPoint->canContainAnyThrowable()) : ThrowPoint::createImplicit($scope, $arg->value), $arrowFunctionResult->getThrowPoints()));
52345234
$impurePoints = array_merge($impurePoints, $arrowFunctionResult->getImpurePoints());
5235+
$isAlwaysTerminating = $isAlwaysTerminating || $arrowFunctionResult->isAlwaysTerminating();
52355236
}
52365237
} else {
52375238
$exprType = $scope->getType($arg->value);

tests/PHPStan/Analyser/ExpressionResultTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ public static function dataIsAlwaysTerminating(): array
8181
'fn() => yield (exit());',
8282
false,
8383
],
84+
[
85+
'(fn() => exit())();', // immediately invoked function expression
86+
true,
87+
],
88+
[
89+
'register_shutdown_function(fn() => exit());',
90+
false,
91+
],
8492
[
8593
'@exit();',
8694
true,
@@ -101,6 +109,10 @@ public static function dataIsAlwaysTerminating(): array
101109
'exit() ?? $x;',
102110
true,
103111
],
112+
[
113+
'call_user_func(fn() => exit());',
114+
true,
115+
],
104116
[
105117
'var_dump(1+exit());',
106118
true,

0 commit comments

Comments
 (0)