From d37f51dabb5de6d26a5886236e726c0ad6407678 Mon Sep 17 00:00:00 2001 From: Richard van Velzen Date: Wed, 1 Jun 2022 08:20:15 +0200 Subject: [PATCH] Improve dead code detection after nested function calls with conditional never return type --- src/Analyser/NodeScopeResolver.php | 8 +++---- .../DeadCode/UnreachableStatementRuleTest.php | 11 +++++++++ .../PHPStan/Rules/DeadCode/data/bug-7188.php | 23 +++++++++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 tests/PHPStan/Rules/DeadCode/data/bug-7188.php diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index 1e7941bd5c..a6a8cbb2d1 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -1661,11 +1661,9 @@ private function findEarlyTerminatingExpr(Expr $expr, Scope $scope): ?Expr return $expr; } - if ($expr instanceof Expr\CallLike || $expr instanceof Expr\Match_) { - $exprType = $scope->getType($expr); - if ($exprType instanceof NeverType && $exprType->isExplicit()) { - return $expr; - } + $exprType = $scope->getType($expr); + if ($exprType instanceof NeverType && $exprType->isExplicit()) { + return $expr; } return null; diff --git a/tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php b/tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php index 32599a1c70..4cf373673f 100644 --- a/tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php +++ b/tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php @@ -120,4 +120,15 @@ public function testBug4370(): void $this->analyse([__DIR__ . '/data/bug-4370.php'], []); } + public function testBug7188(): void + { + $this->treatPhpDocTypesAsCertain = true; + $this->analyse([__DIR__ . '/data/bug-7188.php'], [ + [ + 'Unreachable statement - code above always terminates.', + 22, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/DeadCode/data/bug-7188.php b/tests/PHPStan/Rules/DeadCode/data/bug-7188.php new file mode 100644 index 0000000000..dd607b23c6 --- /dev/null +++ b/tests/PHPStan/Rules/DeadCode/data/bug-7188.php @@ -0,0 +1,23 @@ +