1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Catch unused foreach key when it’s already in scope

This commit is contained in:
Matthew Brown 2018-06-17 00:52:32 -04:00
parent 805bfa6f2d
commit 5ddd74e7d2
2 changed files with 18 additions and 0 deletions

View File

@ -376,6 +376,11 @@ class ForeachChecker
$location,
$foreach_context->branch_point
);
} else {
$statements_checker->registerVariableAssignment(
$key_var_id,
$location
);
}
if ($stmt->byRef && $context->collect_references) {

View File

@ -1314,6 +1314,19 @@ class UnusedVariableTest extends TestCase
echo $a;',
'error_message' => 'UnusedVariable',
],
'reusedKeyVar' => [
'<?php
$key = "a";
echo $key;
$arr = ["foo" => "foo.foo"];
foreach ($arr as $key => $v) {
list($key) = explode(".", $v);
echo $key;
}',
'error_message' => 'UnusedVariable',
],
];
}
}