Skip to content

Commit

Permalink
Fix #1980 - don’t complain about UnusedVariable after continue
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Jul 31, 2019
1 parent f9d9949 commit 3a1a3eb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Psalm/Internal/Analyzer/StatementsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,12 @@ function ($line) {

if ($context->collect_references && (!$context->case_scope || $stmt->num)) {
foreach ($context->unreferenced_vars as $var_id => $locations) {
if (isset($loop_scope->unreferenced_vars[$var_id])) {
$loop_scope->unreferenced_vars[$var_id] += $locations;
} else {
$loop_scope->unreferenced_vars[$var_id] = $locations;
}

if (isset($loop_scope->possibly_unreferenced_vars[$var_id])) {
$loop_scope->possibly_unreferenced_vars[$var_id] += $locations;
} else {
Expand Down
65 changes: 65 additions & 0 deletions tests/UnusedCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,71 @@ public static function bar() : void {}
A::bar();
}',
],
'usedParamInLoopBeforeBreak' => [
'<?php
class Foo {}
function takesFoo(Foo $foo1, Foo $foo2): Foo {
while (rand(0, 1)) {
echo get_class($foo1);
if (rand(0, 1)) {
$foo1 = $foo2;
break;
}
}
return $foo1;
}',
],
'usedParamInLoopBeforeContinue' => [
'<?php
class Foo {}
function takesFoo(Foo $foo1, Foo $foo2): Foo {
while (rand(0, 1)) {
echo get_class($foo1);
if (rand(0, 1)) {
$foo1 = $foo2;
break;
}
}
return $foo1;
}',
],
'usedParamInLoopBeforeWithChangeContinue' => [
'<?php
class Foo {}
class Bar {
public static function build(Foo $foo) : ?self {
echo get_class($foo);
return new self();
}
public function produceFoo(): Foo {
return new Foo();
}
}
function takesFoo(Foo $foo): Foo {
while (rand(0, 1)) {
$bar = Bar::build($foo);
if ($bar) {
$foo = $bar->produceFoo();
continue;
}
}
return $foo;
}',
],
];
}

Expand Down

0 comments on commit 3a1a3eb

Please sign in to comment.