Report variable writes whose value never reaches a use, per array offset - #6334
Open
ondrejmirtes wants to merge 6 commits into
Open
Report variable writes whose value never reaches a use, per array offset#6334ondrejmirtes wants to merge 6 commits into
ondrejmirtes wants to merge 6 commits into
Conversation
ondrejmirtes
force-pushed
the
unused-variables
branch
from
September 7, 2026 15:49
2441298 to
805b8ab
Compare
ondrejmirtes
force-pushed
the
unused-variables-followups
branch
2 times, most recently
from
September 7, 2026 16:33
6041802 to
8e260fa
Compare
ondrejmirtes
force-pushed
the
unused-variables
branch
6 times, most recently
from
September 9, 2026 20:44
f13bef3 to
0a3ccf8
Compare
ondrejmirtes
force-pushed
the
unused-variables-followups
branch
from
September 10, 2026 07:23
8e260fa to
0de7e0c
Compare
ondrejmirtes
force-pushed
the
unused-variables
branch
2 times, most recently
from
September 10, 2026 08:36
5de5f2f to
d21d791
Compare
ondrejmirtes
force-pushed
the
unused-variables-followups
branch
5 times, most recently
from
September 10, 2026 11:48
e76cfae to
8dce798
Compare
ondrejmirtes
force-pushed
the
unused-variables
branch
2 times, most recently
from
September 10, 2026 12:03
4f5a86e to
5195842
Compare
ondrejmirtes
force-pushed
the
unused-variables-followups
branch
from
September 10, 2026 12:05
8dce798 to
157dbbd
Compare
ondrejmirtes
force-pushed
the
unused-variables-followups
branch
from
September 10, 2026 16:05
157dbbd to
ad8ad52
Compare
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.
Follow-up to #6330 (merged): the two refinements listed there, the
unset()false negative, and a few things found while dogfooding.Value flow (Psalm's "better unused variable detection")
A read is no longer a use by itself. A write is used iff its value reaches a sink — a call argument, a condition, a return, echo, throw, a property write… — directly or through the writes it is computed into. So
$a = 5; $a = $a + 1;,$s .= …chains,$i++runs and Psalm's article example are reported at every write, while$a = 5; $a = $a + 1; sink($a);stays quiet.ExpressionContextcarries a value-flow target.AssignHandler,AssignOpHandlerand the inc/dec handlers build the target write up front (VariableFlowBuilder::writeSite()) and walk the value inenterValueFlow(); pure combinators (arithmetic/concat/comparisons, casts, unary ops, ternary and match arms, literal arrays, interpolated strings,??'s right side) keep the target viaenterDeepKeepingValueFlow(), everything else drops it —enterDeep()for nested sub-expressions,withoutValueFlow()for same-depth sinks (&&/||right operands, piped calls, closure uses, assignment-target sub-walks).VariableHandler::composeResult()emits a read flow tagged with the target's id instead of a plain read.VariableLivenessResolverturns those into dependency edges between writes during its backward pass and resolves the used set as a worklist fixpoint from the sinks (resolveDependencies());VariableWritesNodeexposes both answers asisRead()(some read of the value exists) andisUsed()(the value reaches a sink).$a = $b = $c + 1and$a = ($b OP= …): the inner write's inputs are handed to the enclosing expression as aVariableInputFlow— copied to the outer target, or treated as consumed when the enclosing expression is a sink.$a = $b = 1; sink($a);still reports$b(its variable is never read).UnusedParametersCheckandUnusedClosureUsesRuleaskisUsed()too, so a parameter or by-value capture that only feeds unused values is reported.$thisand superglobals are never write sites, so$_GET['x'] = $value;keeps$valueobservable.The message tells the two apart:
… is never read.when nothing reads the written value at all,… is never used.when it is read, but only by expressions computing other values that never reach a sink.Array offsets — literals and
$a['k'] = …$a['k'] = …is an offset write that kills only the earlier writes of offset'k';$a['k']['j'] = …extends offset'k'(reads it, kills nothing);$a[$i] = …/$a[] = …are unknown-offset writes, and are reported when a constant-offset read is the only thing after them ($a[$i] = 1; $a['x'] = 2; return $a['x'];).$a['k']reads offset'k'plus unknown-offset writes; a dynamic offset reads everything;$a['k'] OP=and$a['k'] ??=read the offset first.$a = [1, 2]; $a[0] = 1;reportsOffset $a[0] is assigned value 1 but it already has that value.(nested offsets and coerced keys included; append, dynamic keys, by-reference items and PHPDoc-only certainty are excluded).unset($x)/unset($a['k'])discard the reaching writes without reading them (VariableFlow::discard()), so$x = 1; unset($x);is reported too — unless releasing the value has side effects (objects, resources, arrays that may contain them), where theunset()counts as a read so$cache = new Cache(); unset($cache);stays quiet.Messages:
Value assigned to variable $x is never used.,Value assigned to $a['k'] is never read.(alsoValue of $a['n'] after ++ …,Foreach value $a['x'] …),Offset 'k' of array assigned to variable $a is never used.(array.unusedOffset) — an item is reported on its own only when the array as a whole is used, otherwise the assignment is.Verification
unused-variable-value-flow.php,unused-variable-offsets.php,unused-variable-redundant-offsets.php,unused-variable-offset-overwrite.php,unused-variable-destructor.php, plusunused-input-value-flow.phpfor the parameter and closure-use rules. The #12012 case now reports exactly the lines the reporter asked for.$overloadsinstead of$processedOverloads, andarray-destructuring-array-dim-fetchbuilds$barcodesfor nothing.UnusedVariableRuleTest,UnusedClosureUsesRuleTest,UnusedFunctionParametersRuleTest),make phpstanclean,--group levelsgreen — the one new expectation file (arrayDimFetches-4.json) is part of this PR.ArrayCollection::$elementsdoes not acceptarray<int|TKey, T>), not on this branch.🤖 Generated with Claude Code
https://claude.ai/code/session_01CbZPnVnRJDLbnqmtD3sYSy