1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix a few issues with dead code detection

This commit is contained in:
Matthew Brown 2018-06-16 21:14:19 -04:00
parent 3670f066bb
commit f38da967dd
3 changed files with 64 additions and 2 deletions

View File

@ -460,7 +460,13 @@ class ForeachChecker
);
if ($context->collect_references) {
$context->unreferenced_vars = $foreach_context->unreferenced_vars;
foreach ($foreach_context->unreferenced_vars as $var_id => $locations) {
if (isset($context->unreferenced_vars[$var_id])) {
$context->unreferenced_vars[$var_id] += $locations;
} else {
$context->unreferenced_vars[$var_id] = $locations;
}
}
}
return null;

View File

@ -244,7 +244,7 @@ class TryChecker
$context->referenced_var_ids
);
if ($context->collect_references) {
if ($context->collect_references && $catch_actions[$i] !== [ScopeChecker::ACTION_END]) {
foreach ($context->unreferenced_vars as $var_id => $_) {
if (!isset($catch_context->unreferenced_vars[$var_id])) {
unset($context->unreferenced_vars[$var_id]);

View File

@ -176,6 +176,15 @@ class UnusedVariableTest extends TestCase
}
echo $a[0];',
],
'foreachVarSetInValue' => [
'<?php
/** @param string[] $arr */
function foo(array $arr) : void {
$a = null;
foreach ($arr as $a) { }
if ($a) {}
}',
],
'definedInSecondBranchOfCondition' => [
'<?php
if (rand(0, 1) && $a = rand(0, 1)) {
@ -458,6 +467,30 @@ class UnusedVariableTest extends TestCase
if ($s) {}
}',
],
'throwWithReturnInOneCatch' => [
'<?php
class E1 extends Exception {}
function dangerous(): void {
if (rand(0, 1)) {
throw new \Exception("bad");
}
}
function callDangerous(): void {
try {
dangerous();
$s = true;
} catch (E1 $e) {
echo $e->getMessage();
$s = false;
} catch (Exception $e) {
return;
}
if ($s) {}
}',
],
'loopWithIfRedefinition' => [
'<?php
$i = false;
@ -997,6 +1030,29 @@ class UnusedVariableTest extends TestCase
}',
'error_message' => 'UnusedVariable',
],
'throwWithReturnInOneCatchAndNoReference' => [
'<?php
class E1 extends Exception {}
function dangerous(): void {
if (rand(0, 1)) {
throw new \Exception("bad");
}
}
function callDangerous(): void {
try {
dangerous();
$s = true;
} catch (E1 $e) {
echo $e->getMessage();
$s = false;
} catch (Exception $e) {
return;
}
}',
'error_message' => 'UnusedVariable',
],
'loopTypeChangedInIfWithoutReference' => [
'<?php
$a = false;