Skip RectorCheaperGuardsFirstRule when anchor is not hoist-safe - #281
Merged
Conversation
Only flag a cheap guard for hoisting when the expensive-call anchor merely captures the value (assignment) or bails on it (pure bail guard). An anchor with its own side effects - an attribute-writing block, or a bare call statement that mutates a variable by-reference inside a closure - is unsafe to hoist past, since the nodes the guard bails out on would lose that side effect. Fixes false positives on DowngradeSubstrFalsyRector and TypeWillReturnCallableArrowFunctionRector. Claude-Session: https://claude.ai/code/session_01Na4eYvNqa2pdhZrBXbegrv
This was referenced Aug 29, 2026
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.
RectorCheaperGuardsFirstRule(added in 14.13.0) produced false positives on rules where the cheap guard cannot be hoisted above the expensive call:DowngradeSubstrFalsyRectormarkingIS_FALSY_UNCASTABLEon non-substr call args);TypeWillReturnCallableArrowFunctionRectorwith$hasChanged).Fix: only flag when the anchor is genuinely hoist-safe - either an assignment that just captures the expensive value, or a pure bail guard whose condition holds the expensive call. Anything else is skipped.
Adds two skip fixtures reproducing both patterns; the true-positive fixture still reports. Verified against the real rector-downgrade-php and rector-phpunit files: both pass with no ignore needed.