Skip to content

Read the updated value of consumed prefix mutations - #6692

Open
drewmt wants to merge 3 commits into
phpstan:2.3.xfrom
drewmt:fix/preincrement-value-flow
Open

drewmt wants to merge 3 commits into
phpstan:2.3.xfrom
drewmt:fix/preincrement-value-flow

Conversation

@drewmt

@drewmt drewmt commented Oct 6, 2026 •

Copy link
Copy Markdown

Prefix increment/decrement expressions evaluate to the updated value. Their variable flow currently records only consumption of the mutation's inputs, leaving the new write marked unused even when it is returned, such as return (string) ++$count.

Record a target read after the prefix write, preserving the enclosing value-flow target. This also covers consumed array-offset mutations. Postfix expressions keep their existing pre-mutation input flow, and genuinely unused mutation results remain diagnosed. Mirror the change in the native handlers and bump the expected Turbo version.

Closes phpstan/phpstan#15411.

The regression retains the reported reproducer and covers decrement, assignment to a used result and array offsets. It fails with six unused-value diagnostics before the fix and passes after it.

Validation

  • UnusedVariableRuleTest: 33 tests / 33 assertions, with and without Turbo; includes existing unused prefix/postfix controls.
  • Configured PHP 8.4 unit suite, with and without Turbo: 22,450 tests / 96,879 assertions / 106 skips, no failures.
  • PHP 8.3 unit suite: 22,303 tests / 96,630 assertions / 204 skips, no failures.
  • Strict native build, signature parity (16,390 members), differential smoke (ALL OK) and method/declaration parity on PHP 8.3 pass.
  • PHP/native walk traces match across dead-code fixtures and prefix/condition type-inference fixtures (23,342 lines). Raw diagnostics and stderr match byte-for-byte with bleeding edge enabled.
  • Full configured self-analysis with Turbo, scoped PHPStan without Turbo, four-file coding standards, PHP syntax and whitespace checks pass. Full integration and cross-platform CI have not run locally.

Reproduce the rule regression with php vendor/bin/phpunit tests/PHPStan/Rules/DeadCode/UnusedVariableRuleTest.php --filter testBug15411.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive for preInc.unused after upgrading to 2.3.0

1 participant