1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix #1598, catching unused variables followed by try inside loop

This commit is contained in:
Brown 2019-05-02 17:21:02 -04:00
parent 86bf159bd0
commit 08bf101f48
3 changed files with 13 additions and 2 deletions

View File

@ -342,7 +342,7 @@ class TryAnalyzer
if ($catch_actions[$i] !== [ScopeAnalyzer::ACTION_END]) { if ($catch_actions[$i] !== [ScopeAnalyzer::ACTION_END]) {
foreach ($catch_context->vars_in_scope as $var_id => $type) { foreach ($catch_context->vars_in_scope as $var_id => $type) {
if ($context->hasVariable($var_id) if (isset($context->vars_in_scope[$var_id])
&& $context->vars_in_scope[$var_id]->getId() !== $type->getId() && $context->vars_in_scope[$var_id]->getId() !== $type->getId()
) { ) {
$context->vars_in_scope[$var_id] = Type::combineUnionTypes( $context->vars_in_scope[$var_id] = Type::combineUnionTypes(

View File

@ -294,7 +294,7 @@ class UnusedCodeTest extends TestCase
public function bar() : void { public function bar() : void {
$c = $this->getC(); $c = $this->getC();
foreach ([1, 2, 3] as $i) { foreach ([1, 2, 3] as $_) {
try { try {
$c->foo(); $c->foo();
} catch (Exception $e) {} } catch (Exception $e) {}

View File

@ -1427,6 +1427,17 @@ class UnusedVariableTest extends TestCase
}', }',
'error_message' => 'UnusedVariable', 'error_message' => 'UnusedVariable',
], ],
'detectUnusedVarBeforeTryInsideForeach' => [
'<?php
function foo() : void {
$unused = 1;
while (rand(0, 1)) {
try {} catch (\Exception $e) {}
}
}',
'error_message' => 'UnusedVariable',
],
]; ];
} }
} }