Skip to content

[DeadCode] Skip RemoveDeadIfBlockRector on empty if with elseif and trailing else - #8446

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-remove-dead-if-block-else
Sep 3, 2026
Merged

[DeadCode] Skip RemoveDeadIfBlockRector on empty if with elseif and trailing else#8446
TomasVotruba merged 1 commit into
mainfrom
fix-remove-dead-if-block-else

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Fixes rectorphp/rector#9884

RemoveDeadIfBlockRector changed behavior on an empty if body followed by elseif and a trailing else:

if (in_array($a, $b)) {
} elseif (in_array($a, $c)) {
    echo "elseif";
} else {
    echo "else";
}

was merged into:

if (!in_array($a, $b) && in_array($a, $c)) {
    echo "elseif";
} else {
    echo "else";
}

When the original if condition was true, the empty body ran nothing. After the merge the negated condition is false, so the else branch runs instead - a semantic change.

The merge is only safe when there is no trailing else. This bails out and keeps the code unchanged when an else is present. The existing empty_if_with_elseif_and_else.php.inc fixture (which encoded the wrong output) becomes a skip_ fixture.

https://claude.ai/code/session_01MszFhfN8nK7KR3mBjda8P2

…railing else

An empty if body followed by elseif and else was merged into
"if (!cond && elseifCond) {...} else {...}". When the original if
condition was true, the negated condition made the else branch run,
which the empty if body never did. Keep such code unchanged.

Fixes rectorphp/rector#9884

Claude-Session: https://claude.ai/code/session_01MszFhfN8nK7KR3mBjda8P2
@TomasVotruba
TomasVotruba force-pushed the fix-remove-dead-if-block-else branch from 4a5fa0d to 2417780 Compare September 3, 2026 19:48
@TomasVotruba
TomasVotruba merged commit dc1d9e7 into main Sep 3, 2026
43 checks passed
@TomasVotruba
TomasVotruba deleted the fix-remove-dead-if-block-else branch September 3, 2026 19:49
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.

RemoveDeadIfBlockRector changes behaviour with elseif

1 participant