diff --git a/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php b/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php index e63f71354..10ea73806 100644 --- a/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php @@ -359,41 +359,10 @@ class StatementsAnalyzer extends SourceAnalyzer $codebase = $statements_analyzer->getCodebase(); - if ($context->has_returned - && !$context->collect_initializations - && !$context->collect_mutations - && !($stmt instanceof PhpParser\Node\Stmt\Nop) - && !($stmt instanceof PhpParser\Node\Stmt\Function_) - && !($stmt instanceof PhpParser\Node\Stmt\Class_) - && !($stmt instanceof PhpParser\Node\Stmt\Interface_) - && !($stmt instanceof PhpParser\Node\Stmt\Trait_) - && !($stmt instanceof PhpParser\Node\Stmt\HaltCompiler) - ) { - if ($codebase->find_unused_variables) { - if (IssueBuffer::accepts( - new UnevaluatedCode( - 'Expressions after return/throw/continue', - new CodeLocation($statements_analyzer->source, $stmt) - ), - $statements_analyzer->source->getSuppressedIssues() - )) { - return null; - } - } - - return null; - } - if ($statements_analyzer->getProjectAnalyzer()->debug_lines) { fwrite(STDERR, $statements_analyzer->getFilePath() . ':' . $stmt->getLine() . "\n"); } - /* - if (isset($context->vars_in_scope['$array']) && !$stmt instanceof PhpParser\Node\Stmt\Nop) { - var_dump($stmt->getLine(), $context->vars_in_scope['$array']); - } - */ - $new_issues = null; $traced_variables = []; @@ -517,6 +486,31 @@ class StatementsAnalyzer extends SourceAnalyzer $statements_analyzer->parsed_docblock = null; } + if ($context->has_returned + && !$context->collect_initializations + && !$context->collect_mutations + && !($stmt instanceof PhpParser\Node\Stmt\Nop) + && !($stmt instanceof PhpParser\Node\Stmt\Function_) + && !($stmt instanceof PhpParser\Node\Stmt\Class_) + && !($stmt instanceof PhpParser\Node\Stmt\Interface_) + && !($stmt instanceof PhpParser\Node\Stmt\Trait_) + && !($stmt instanceof PhpParser\Node\Stmt\HaltCompiler) + ) { + if ($codebase->find_unused_variables) { + if (IssueBuffer::accepts( + new UnevaluatedCode( + 'Expressions after return/throw/continue', + new CodeLocation($statements_analyzer->source, $stmt) + ), + $statements_analyzer->source->getSuppressedIssues() + )) { + return null; + } + } + + return null; + } + if ($stmt instanceof PhpParser\Node\Stmt\If_) { if (IfElseAnalyzer::analyze($statements_analyzer, $stmt, $context) === false) { return false; diff --git a/tests/IssueSuppressionTest.php b/tests/IssueSuppressionTest.php index fe65d31d9..af47b9532 100644 --- a/tests/IssueSuppressionTest.php +++ b/tests/IssueSuppressionTest.php @@ -18,6 +18,12 @@ class IssueSuppressionTest extends TestCase use ValidCodeAnalysisTestTrait; use InvalidCodeAnalysisTestTrait; + public function setUp(): void + { + parent::setUp(); + $this->project_analyzer->getCodebase()->find_unused_variables = true; + } + public function testIssueSuppressedOnFunction(): void { $this->expectException(CodeException::class); @@ -162,8 +168,6 @@ class IssueSuppressionTest extends TestCase $this->addFile( getcwd() . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'somefile.php', ' [ @@ -319,14 +326,14 @@ class IssueSuppressionTest extends TestCase 'suppressAllStatementIssues' => [ ' [ ' [ @@ -390,6 +397,15 @@ class IssueSuppressionTest extends TestCase } ', ], + 'suppressUnevaluatedCode' => [ + '