From cca0070d38fec59c1759a741f4eea2f2ae611023 Mon Sep 17 00:00:00 2001 From: Brown Date: Wed, 2 Jan 2019 17:16:04 -0500 Subject: [PATCH] Allow function calls that exit to act themselves like exit --- .../FunctionLike/ReturnTypeCollector.php | 8 ++++++-- src/Psalm/Internal/Analyzer/ScopeAnalyzer.php | 8 ++++++++ tests/ReturnTypeTest.php | 16 ++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeCollector.php b/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeCollector.php index 6a00a1ef4..c257f8783 100644 --- a/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeCollector.php +++ b/src/Psalm/Internal/Analyzer/FunctionLike/ReturnTypeCollector.php @@ -54,12 +54,16 @@ class ReturnTypeCollector } break; - } elseif ($stmt instanceof PhpParser\Node\Stmt\Throw_ + } + + if ($stmt instanceof PhpParser\Node\Stmt\Throw_ || $stmt instanceof PhpParser\Node\Stmt\Break_ || $stmt instanceof PhpParser\Node\Stmt\Continue_ ) { break; - } elseif ($stmt instanceof PhpParser\Node\Stmt\Expression + } + + if ($stmt instanceof PhpParser\Node\Stmt\Expression && ($stmt->expr instanceof PhpParser\Node\Expr\Yield_ || $stmt->expr instanceof PhpParser\Node\Expr\YieldFrom) ) { diff --git a/src/Psalm/Internal/Analyzer/ScopeAnalyzer.php b/src/Psalm/Internal/Analyzer/ScopeAnalyzer.php index b2fdf77f2..a3f471fc3 100644 --- a/src/Psalm/Internal/Analyzer/ScopeAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/ScopeAnalyzer.php @@ -100,6 +100,14 @@ class ScopeAnalyzer return [self::ACTION_END]; } + // This allows calls to functions that always exit to act as exit statements themselves + if (!$return_is_exit + && isset($stmt->expr->inferredType) + && $stmt->expr->inferredType->isNever() + ) { + return [self::ACTION_END]; + } + if ($exit_functions) { if ($stmt->expr instanceof PhpParser\Node\Expr\FuncCall || $stmt->expr instanceof PhpParser\Node\Expr\StaticCall diff --git a/tests/ReturnTypeTest.php b/tests/ReturnTypeTest.php index 3d6de2b51..5f832abe4 100644 --- a/tests/ReturnTypeTest.php +++ b/tests/ReturnTypeTest.php @@ -568,6 +568,22 @@ class ReturnTypeTest extends TestCase } }', ], + 'noReturnCallReturns' => [ + '