Skip to content
Permalink
Browse files

Fix #2007 - improve unused variable detection in always-entered loops

  • Loading branch information...
muglug committed Aug 12, 2019
1 parent 669b50e commit 10b8a94564d2a632554eb03ba9d0bea9b178d8de
@@ -291,6 +291,13 @@ public static function analyze(
);
}
} else {
if ($context->collect_references
&& !isset($context->vars_in_scope[$var_id])
&& isset($inner_loop_context->unreferenced_vars[$var_id])
) {
$context->unreferenced_vars[$var_id] = $inner_loop_context->unreferenced_vars[$var_id];
}
$context->vars_in_scope[$var_id] = $type;
}
}
@@ -273,8 +273,9 @@ public static function analyze(
}
if ($inner_context->collect_references) {
foreach ($inner_context->unreferenced_vars as $var_id => $_) {
foreach ($inner_context->unreferenced_vars as $var_id => $locations) {
if (!isset($pre_outer_context->vars_in_scope[$var_id])) {
$loop_scope->unreferenced_vars[$var_id] = $locations;
unset($inner_context->unreferenced_vars[$var_id]);
}
}
@@ -1034,6 +1034,18 @@ public function foo() : void {
}
}'
],
'setInLoopThatsAlwaysEntered' => [
'<?php
/**
* @param non-empty-array<int> $a
*/
function getLastNum(array $a): int {
foreach ($a as $num) {
$last = $num;
}
return $last;
}'
],
];
}
@@ -1712,6 +1724,19 @@ function foo() : void {
}',
'error_message' => 'UnusedVariable',
],
'setInLoopThatsAlwaysEnteredButNotReferenced' => [
'<?php
/**
* @param non-empty-array<int> $a
*/
function getLastNum(array $a): int {
foreach ($a as $num) {
$last = $num;
}
return 4;
}',
'error_message' => 'UnusedVariable',
],
];
}
}

0 comments on commit 10b8a94

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