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

Fix #4839 - undefined possibly set in by-reference should be treated as such

This commit is contained in:
Matt Brown 2020-12-14 16:57:48 -05:00
parent 2021c81606
commit e43f2259ea
2 changed files with 38 additions and 1 deletions

View File

@ -1186,6 +1186,8 @@ class AssignmentAnalyzer
if (!$context->hasVariable($var_id)) { if (!$context->hasVariable($var_id)) {
$context->vars_possibly_in_scope[$var_id] = true; $context->vars_possibly_in_scope[$var_id] = true;
$location = new CodeLocation($statements_analyzer->getSource(), $stmt);
if (!$statements_analyzer->hasVariable($var_id)) { if (!$statements_analyzer->hasVariable($var_id)) {
if ($constrain_type if ($constrain_type
&& $prevent_null && $prevent_null
@ -1197,7 +1199,7 @@ class AssignmentAnalyzer
if (IssueBuffer::accepts( if (IssueBuffer::accepts(
new \Psalm\Issue\NullReference( new \Psalm\Issue\NullReference(
'Not expecting null argument passed by reference', 'Not expecting null argument passed by reference',
new CodeLocation($statements_analyzer->getSource(), $stmt) $location
), ),
$statements_analyzer->getSuppressedIssues() $statements_analyzer->getSuppressedIssues()
)) { )) {
@ -1205,6 +1207,24 @@ class AssignmentAnalyzer
} }
} }
if ($stmt instanceof PhpParser\Node\Expr\Variable) {
$statements_analyzer->registerVariable(
$var_id,
$location,
$context->branch_point
);
if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph) {
$byref_node = DataFlowNode::getForAssignment($var_id, $location);
$statements_analyzer->data_flow_graph->addPath(
$byref_node,
new DataFlowNode('variable-use', 'variable use', null),
'variable-use'
);
}
}
$context->hasVariable($var_id); $context->hasVariable($var_id);
} else { } else {
$var_not_in_scope = true; $var_not_in_scope = true;

View File

@ -451,6 +451,23 @@ class ArgTest extends TestCase
false, false,
'8.0' '8.0'
], ],
'byrefVarSetsPossible' => [
'<?php
/**
* @param mixed $a
* @psalm-param-out int $a
*/
function takesByRef(&$a) : void {
$a = 5;
}
if (rand(0, 1)) {
takesByRef($b);
}
echo $b;',
'error_message' => 'PossiblyUndefinedGlobalVariable',
],
]; ];
} }
} }