Skip to content

[CodeQuality] Skip same boolean in both branches in SimplifyIfReturnBoolRector#8159

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-simplify-if-return-bool-same-bool
Jul 8, 2026
Merged

[CodeQuality] Skip same boolean in both branches in SimplifyIfReturnBoolRector#8159
TomasVotruba merged 1 commit into
mainfrom
fix-simplify-if-return-bool-same-bool

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Jul 8, 2026

Copy link
Copy Markdown
Member

Fixes rectorphp/rector#9798

When both the if branch and the trailing statement return the same boolean, the condition does not affect the result, yet SimplifyIfReturnBoolRector rewrote it using the condition — producing a wrong result and dropping the condition's side effect.

Before (buggy output)

function test(): bool
{
    if (! return_false()) {
        return true;
    }

    return true;
}

was rewritten to:

function test(): bool
{
    return return_false(); // wrong — original always returns true
}

After

Left unchanged — both branches return the same boolean, so the rule now bails out.

Fix

shouldSkipIfAndReturn() now skips when the inner return and the trailing return are the same boolean value.

@TomasVotruba TomasVotruba force-pushed the fix-simplify-if-return-bool-same-bool branch from 3ed0fe8 to 85dab1d Compare July 8, 2026 16:28
@TomasVotruba TomasVotruba changed the title [CodeQuality] Skip same boolean in both branches in SimplifyIfReturnBoolRector [CodeQuality] Collapse same boolean in both branches to direct return in SimplifyIfReturnBoolRector Jul 8, 2026
@TomasVotruba TomasVotruba force-pushed the fix-simplify-if-return-bool-same-bool branch from 85dab1d to 7e49e69 Compare July 8, 2026 16:30
@TomasVotruba TomasVotruba changed the title [CodeQuality] Collapse same boolean in both branches to direct return in SimplifyIfReturnBoolRector [CodeQuality] Skip same boolean in both branches in SimplifyIfReturnBoolRector Jul 8, 2026
@TomasVotruba TomasVotruba enabled auto-merge (squash) July 8, 2026 16:31
@TomasVotruba TomasVotruba merged commit 52b57a6 into main Jul 8, 2026
65 checks passed
@TomasVotruba TomasVotruba deleted the fix-simplify-if-return-bool-same-bool branch July 8, 2026 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

SimplifyIfReturnBoolRector

1 participant