1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix #2574 - remove assertions after increment

This commit is contained in:
Brown 2020-01-09 11:51:49 -05:00
parent bd7f342491
commit bd9142f131
3 changed files with 79 additions and 1 deletions

View File

@ -283,7 +283,7 @@ class AssignmentAnalyzer
$assign_value_type->by_ref = true;
}
// removes dependennt vars from $context
// removes dependent vars from $context
$context->removeDescendents(
$array_var_id,
$context->vars_in_scope[$array_var_id],
@ -978,6 +978,34 @@ class AssignmentAnalyzer
}
}
if ($array_var_id && isset($context->vars_in_scope[$array_var_id])) {
if ($result_type && $context->vars_in_scope[$array_var_id]->by_ref) {
$result_type->by_ref = true;
}
// removes dependent vars from $context
$context->removeDescendents(
$array_var_id,
$context->vars_in_scope[$array_var_id],
$result_type,
$statements_analyzer
);
} else {
$root_var_id = ExpressionAnalyzer::getRootVarId(
$stmt->var,
$statements_analyzer->getFQCLN(),
$statements_analyzer
);
if ($root_var_id && isset($context->vars_in_scope[$root_var_id])) {
$context->removeVarFromConflictingClauses(
$root_var_id,
$context->vars_in_scope[$root_var_id],
$statements_analyzer
);
}
}
if ($stmt->var instanceof PhpParser\Node\Expr\ArrayDimFetch) {
ArrayAssignmentAnalyzer::analyze(
$statements_analyzer,

View File

@ -412,6 +412,14 @@ class ExpressionAnalyzer
);
$context->unreferenced_vars[$var_id] = [$location->getHash() => $location];
}
// removes dependent vars from $context
$context->removeDescendents(
$var_id,
$context->vars_in_scope[$var_id],
$return_type,
$statements_analyzer
);
}
} else {
$statements_analyzer->node_data->setType($stmt, Type::getMixed());

View File

@ -704,6 +704,48 @@ class RedundantConditionTest extends \Psalm\Tests\TestCase
}
}'
],
'invalidateAfterPostIncrement' => [
'<?php
/**
* @param array<int, int> $tokens
*/
function propertyInUse(array $tokens, int $i): bool {
if ($tokens[$i] !== 1) {
return false;
}
$i++;
if ($tokens[$i] !== 2) {}
return false;
}'
],
'invalidateAfterAssignOp' => [
'<?php
/**
* @param array<int, int> $tokens
*/
function propertyInUse(array $tokens, int $i): bool {
if ($tokens[$i] !== 1) {
return false;
}
$i += 1;
if ($tokens[$i] !== 2) {}
return false;
}'
],
'invalidateAfterAssign' => [
'<?php
/**
* @param array<int, int> $tokens
*/
function propertyInUse(array $tokens, int $i): bool {
if ($tokens[$i] !== 1) {
return false;
}
$i = $i + 1;
if ($tokens[$i] !== 2) {}
return false;
}'
],
];
}