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

Fix #2244 - correctly check do while expression before use

This commit is contained in:
Matthew Brown 2019-10-20 14:18:30 -04:00
parent 3bb0016434
commit 4aeb28aceb
2 changed files with 63 additions and 25 deletions

View File

@ -110,17 +110,16 @@ class LoopAnalyzer
$old_referenced_var_ids = $inner_context->referenced_var_ids; $old_referenced_var_ids = $inner_context->referenced_var_ids;
$inner_context->referenced_var_ids = []; $inner_context->referenced_var_ids = [];
if (!$is_do) {
foreach ($pre_conditions as $pre_condition) { foreach ($pre_conditions as $pre_condition) {
self::applyPreConditionToLoopContext( self::applyPreConditionToLoopContext(
$statements_analyzer, $statements_analyzer,
$pre_condition, $pre_condition,
$pre_condition_clauses, $pre_condition_clauses,
$inner_context, $inner_context,
$loop_scope->loop_parent_context $loop_scope->loop_parent_context,
$is_do
); );
} }
}
$inner_context->protected_var_ids = $loop_scope->protected_var_ids; $inner_context->protected_var_ids = $loop_scope->protected_var_ids;
@ -154,7 +153,6 @@ class LoopAnalyzer
IssueBuffer::startRecording(); IssueBuffer::startRecording();
if (!$is_do) {
foreach ($pre_conditions as $pre_condition) { foreach ($pre_conditions as $pre_condition) {
$asserted_var_ids = array_merge( $asserted_var_ids = array_merge(
self::applyPreConditionToLoopContext( self::applyPreConditionToLoopContext(
@ -162,12 +160,12 @@ class LoopAnalyzer
$pre_condition, $pre_condition,
$pre_condition_clauses, $pre_condition_clauses,
$loop_scope->loop_context, $loop_scope->loop_context,
$loop_scope->loop_parent_context $loop_scope->loop_parent_context,
$is_do
), ),
$asserted_var_ids $asserted_var_ids
); );
} }
}
// record all the vars that existed before we did the first pass through the loop // record all the vars that existed before we did the first pass through the loop
$pre_loop_context = clone $loop_scope->loop_context; $pre_loop_context = clone $loop_scope->loop_context;
@ -315,7 +313,8 @@ class LoopAnalyzer
$pre_condition, $pre_condition,
$pre_condition_clauses, $pre_condition_clauses,
$inner_context, $inner_context,
$loop_scope->loop_parent_context $loop_scope->loop_parent_context,
false
); );
} }
@ -547,13 +546,28 @@ class LoopAnalyzer
PhpParser\Node\Expr $pre_condition, PhpParser\Node\Expr $pre_condition,
array $pre_condition_clauses, array $pre_condition_clauses,
Context $loop_context, Context $loop_context,
Context $outer_context Context $outer_context,
bool $is_do
) { ) {
$pre_referenced_var_ids = $loop_context->referenced_var_ids; $pre_referenced_var_ids = $loop_context->referenced_var_ids;
$loop_context->referenced_var_ids = []; $loop_context->referenced_var_ids = [];
$loop_context->inside_conditional = true; $loop_context->inside_conditional = true;
$suppressed_issues = $statements_analyzer->getSuppressedIssues();
if ($is_do) {
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
$statements_analyzer->addSuppressedIssues(['RedundantCondition']);
}
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
$statements_analyzer->addSuppressedIssues(['RedundantConditionGivenDocblockType']);
}
if (!in_array('TypeDoesNotContainType', $suppressed_issues, true)) {
$statements_analyzer->addSuppressedIssues(['TypeDoesNotContainType']);
}
}
if (ExpressionAnalyzer::analyze($statements_analyzer, $pre_condition, $loop_context) === false) { if (ExpressionAnalyzer::analyze($statements_analyzer, $pre_condition, $loop_context) === false) {
return []; return [];
} }
@ -591,6 +605,22 @@ class LoopAnalyzer
$loop_context->vars_in_scope = $pre_condition_vars_in_scope_reconciled; $loop_context->vars_in_scope = $pre_condition_vars_in_scope_reconciled;
} }
if ($is_do) {
if (!in_array('RedundantCondition', $suppressed_issues, true)) {
$statements_analyzer->removeSuppressedIssues(['RedundantCondition']);
}
if (!in_array('RedundantConditionGivenDocblockType', $suppressed_issues, true)) {
$statements_analyzer->removeSuppressedIssues(['RedundantConditionGivenDocblockType']);
}
if (!in_array('TypeDoesNotContainType', $suppressed_issues, true)) {
$statements_analyzer->removeSuppressedIssues(['TypeDoesNotContainType']);
}
}
if ($is_do) {
return [];
}
foreach ($asserted_var_ids as $var_id) { foreach ($asserted_var_ids as $var_id) {
$loop_context->clauses = Context::filterClauses( $loop_context->clauses = Context::filterClauses(
$var_id, $var_id,

View File

@ -1125,6 +1125,14 @@ class UnusedVariableTest extends TestCase
echo $s; echo $s;
}', }',
], ],
'doWhileReassignedInConditional' => [
'<?php
$index = 0;
do {
echo $index;
} while (($index = $index + 1) < 10);'
],
]; ];
} }