Skip to content

[CodeQuality] Keep parentheses around Ternary operand in SimplifyConditionsRector#8122

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-simplify-conditions-ternary-parentheses
Jun 30, 2026
Merged

[CodeQuality] Keep parentheses around Ternary operand in SimplifyConditionsRector#8122
TomasVotruba merged 1 commit into
mainfrom
fix-simplify-conditions-ternary-parentheses

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Fixes #8121.

SimplifyConditionsRector inverts a binary op when wrapped in a boolean not, e.g. ! (1 < X)1 >= X. When the operand X is a ternary, the format-preserving printer dropped the parentheses. Since the ternary has lower precedence than >=, the result changed meaning:

// before (input) — evaluates false
if (! (1 < (1 ? 2 : 1))) {}

// produced — evaluates true (wrong)
if (1 >= 1 ? 2 : 1) {}

Now createInversedBooleanOp() sets WRAPPED_IN_PARENTHESES on ternary operands, matching the existing precedent in UseIdenticalOverEqualWithSameTypeRector, producing the correct:

if (1 >= (1 ? 2 : 1)) {}

Bug report and failing fixture by @j-dobr in #8121.

Co-authored-by: Jan Dobrovodsky jan.dobrovodsky@promedcs.com

…itionsRector

Fixes #8121 - inverting a binary op whose operand is a ternary dropped the parentheses, changing evaluation due to lower ternary precedence.
@TomasVotruba

Copy link
Copy Markdown
Member Author

Thank you for reporting 👍

@TomasVotruba TomasVotruba merged commit 647f476 into main Jun 30, 2026
65 checks passed
@TomasVotruba TomasVotruba deleted the fix-simplify-conditions-ternary-parentheses branch June 30, 2026 11:30
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.

1 participant