diff --git a/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php b/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php index ee844f34b..368a4ca88 100644 --- a/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/StatementsAnalyzer.php @@ -2240,7 +2240,7 @@ class StatementsAnalyzer extends SourceAnalyzer implements StatementsSource */ public function getUnusedVarLocations() { - return $this->unused_var_locations; + return array_diff_key($this->unused_var_locations, $this->used_var_locations); } /** diff --git a/tests/UnusedVariableTest.php b/tests/UnusedVariableTest.php index eec86ff4b..d62241824 100644 --- a/tests/UnusedVariableTest.php +++ b/tests/UnusedVariableTest.php @@ -1439,6 +1439,31 @@ class UnusedVariableTest extends TestCase if ($f) {} }' ], + 'usedParamInWhileDirectly' => [ + '= $index = nextNumber($index)) { + // ... + } + } + + function nextNumber(int $eee): int { + return $eee + 1; + }' + ], + 'usedParamInWhileIndirectly' => [ + '= $index = nextNumber($index)) { + // ... + } + } + + function nextNumber(int $i): int { + return $i + 1; + }' + ], ]; }