Report loop variable overwrites only for variables still in use - #333
Merged
Merged
Conversation
Foreach key/value and for-loop init overwrites are reported only when the loop takes over a variable assigned before it and read after it, as answered by VariableWritesNode::getVariableOverwritingLoop(). Reusing a spent loop variable no longer reports. Closes #332 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LYiPGg9cpsKLyK6X5BrJTT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #332, fixes phpstan/phpstan#9940. Requires phpstan/phpstan-src#6414 (merged, in the current 2.3.x dev phar).
OverwriteVariablesWithForeachRuleandOverwriteVariablesWithForLoopInitRulenow listen onVariableWritesNodeand report a foreach key/value binding or a for-loop initial assignment only whengetVariableOverwritingLoop()returns the loop: the variable was assigned before the loop and is read after it, with no assignment in between other than the loop's own bindings and updates. Reusing a spent loop variable is silent, including the flag-then-check pattern from #332 and the #9940 reproducer.Behaviour changes beyond the issue:
if ($c) { $x = 1; }) now reports when$xis read after the loop; it wasMaybeand silent before. The old value genuinely survives an empty loop there.VariableWritesNodeis emitted per function-like, same as phpstan-src's dead-code rules.for ($i = 0; ...)over an existing$ionly when$iis read after the loop.Existing fixtures keep every original error (reads were added after the loops); new fixtures show the silent cases and the positive contrasts (nested loop clobbering an outer loop variable, read in a later iteration of an outer loop, list targets).
The static analysis job needs phpstan/phpstan-src#6415 (
@apion the two classes) in the dev phar; tests and coding standards pass with the current one.🤖 Generated with Claude Code
https://claude.ai/code/session_01LYiPGg9cpsKLyK6X5BrJTT