Skip to content

Commit

Permalink
Fix #1598, catching unused variables followed by try inside loop
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed May 2, 2019
1 parent 86bf159 commit 08bf101
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ function ($fq_catch_class) use ($codebase) {

if ($catch_actions[$i] !== [ScopeAnalyzer::ACTION_END]) {
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] = Type::combineUnionTypes(
Expand Down
2 changes: 1 addition & 1 deletion tests/UnusedCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ class B extends A {
public function bar() : void {
$c = $this->getC();
foreach ([1, 2, 3] as $i) {
foreach ([1, 2, 3] as $_) {
try {
$c->foo();
} catch (Exception $e) {}
Expand Down
11 changes: 11 additions & 0 deletions tests/UnusedVariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,17 @@ function callDangerous(): void {
}',
'error_message' => 'UnusedVariable',
],
'detectUnusedVarBeforeTryInsideForeach' => [
'<?php
function foo() : void {
$unused = 1;
while (rand(0, 1)) {
try {} catch (\Exception $e) {}
}
}',
'error_message' => 'UnusedVariable',
],
];
}
}

0 comments on commit 08bf101

Please sign in to comment.