1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Fix #2197 - prevent false positive from try in while

This commit is contained in:
Matthew Brown 2019-11-09 11:01:48 -05:00
parent 7028a64440
commit 62a3598ed3
2 changed files with 37 additions and 0 deletions

View File

@ -414,6 +414,20 @@ class TryAnalyzer
$statements_analyzer->registerVariableUses($locations);
}
}
$newly_unreferenced_vars = array_merge(
$newly_unreferenced_vars,
array_diff_key(
$try_context->unreferenced_vars,
$old_unreferenced_vars
)
);
foreach ($newly_unreferenced_vars as $var_id => $locations) {
if (!isset($context->unreferenced_vars[$var_id])) {
$context->unreferenced_vars[$var_id] = $locations;
}
}
}
if ($stmt->finally) {

View File

@ -1171,6 +1171,29 @@ class UnusedVariableTest extends TestCase
echo $b;
}'
],
'tryWithWhile' => [
'<?php
function foo(): void {
$done = false;
while (!$done) {
try {
$done = true;
} catch (\Exception $e) {
}
}
}',
],
'tryWithWhileWithoutTry' => [
'<?php
function foo(): void {
$done = false;
while (!$done) {
$done = true;
}
}',
],
];
}