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

Fix #1899 - detect dead code after array assignment

This commit is contained in:
Matthew Brown 2019-07-04 16:38:31 -04:00
parent 802016757c
commit d0f6f85dba
4 changed files with 40 additions and 7 deletions

View File

@ -496,11 +496,7 @@ class ArrayAssignmentAnalyzer
}
}
} elseif ($root_var_id) {
if ($context->hasVariable($root_var_id, $statements_analyzer)) {
$context->vars_in_scope[$root_var_id] = $root_type;
} else {
$context->vars_in_scope[$root_var_id] = $root_type;
}
$context->vars_in_scope[$root_var_id] = $root_type;
}
return null;

View File

@ -147,7 +147,7 @@ class VariableFetchAnalyzer
$var_name = '$' . $stmt->name;
if (!$context->hasVariable($var_name, $statements_analyzer)) {
if (!$context->hasVariable($var_name, !$array_assignment ? $statements_analyzer : null)) {
if (!isset($context->vars_possibly_in_scope[$var_name]) ||
!$statements_analyzer->getFirstAppearance($var_name)
) {

View File

@ -504,7 +504,36 @@ class FunctionClassStringTemplateTest extends TestCase
f(A::class);
f(B::class);',
],
],
'compareToExactClassString' => [
'<?php
/**
* @template T as object
*/
class Type
{
/** @var class-string<T> */
private $typeName;
/**
* @param class-string<T> $typeName
*/
public function __construct(string $typeName) {
$this->typeName = $typeName;
}
/**
* @param mixed $value
* @return T
*/
public function cast($value) {
if (is_object($value) && get_class($value) === $this->typeName) {
return $value;
}
throw new RuntimeException();
}
}'
],
];
}

View File

@ -1630,6 +1630,14 @@ class UnusedVariableTest extends TestCase
}',
'error_message' => 'UnusedVariable',
],
'detectUselessArrayAssignment' => [
'<?php
function foo() : void {
$a = [];
$a[0] = 1;
}',
'error_message' => 'UnusedVariable',
],
];
}
}