Skip to content
Permalink
Browse files

Fix #1980 - don’t complain about UnusedVariable after continue

  • Loading branch information...
muglug committed Jul 31, 2019
1 parent f9d9949 commit 3a1a3ebc47949bdf03cb2c409a6c981414c95b1e
Showing with 71 additions and 0 deletions.
  1. +6 −0 src/Psalm/Internal/Analyzer/StatementsAnalyzer.php
  2. +65 −0 tests/UnusedCodeTest.php
@@ -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 {
@@ -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;
}',
],
];
}

0 comments on commit 3a1a3eb

Please sign in to comment.
You can’t perform that action at this time.