diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/ArrayAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/ArrayAnalyzer.php index 5527cadf8..42f0b6b9d 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/ArrayAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/ArrayAnalyzer.php @@ -232,11 +232,11 @@ class ArrayAnalyzer PhpParser\Node\Expr\ArrayItem $item, Codebase $codebase ) : void { - if (ExpressionAnalyzer::analyze($statements_analyzer, $item->value, $context) === false) { - return; - } - if ($item->unpack) { + if (ExpressionAnalyzer::analyze($statements_analyzer, $item->value, $context) === false) { + return; + } + $unpacked_array_type = $statements_analyzer->node_data->getType($item->value); if (!$unpacked_array_type) { @@ -335,6 +335,10 @@ class ArrayAnalyzer $array_creation_info->item_key_atomic_types[] = new Type\Atomic\TLiteralInt($item_key_value); } + if (ExpressionAnalyzer::analyze($statements_analyzer, $item->value, $context) === false) { + return; + } + $array_creation_info->all_list = $array_creation_info->all_list && $item_is_list_item; if ($item_key_value !== null) { diff --git a/tests/UnusedVariableTest.php b/tests/UnusedVariableTest.php index 93048b4bf..555ea2ce1 100644 --- a/tests/UnusedVariableTest.php +++ b/tests/UnusedVariableTest.php @@ -2392,6 +2392,16 @@ class UnusedVariableTest extends TestCase return implode("/", $arr); }' ], + 'initVariableInOffset' => [ + ' $b, + ]; + + foreach ($a as $key => $value) { + echo $key . " " . $value; + }', + ], ]; }