[CodeQuality] Preserve parentheses around low-precedence operands in LogicalToBooleanRector#8155
Merged
Merged
Conversation
…LogicalToBooleanRector Fixes rectorphp/rector#9800 Boolean operators (&& ||) bind tighter than logical ones (and or). When LogicalToBooleanRector swaps them, operands whose top operator has lower precedence (ternary, elvis, +=/??=, print, yield, yield from) lost their parentheses on reprint, changing semantics. Reprint those operands so the pretty-printer re-adds the parentheses; yield <value> additionally needs them forced, as the format-preserving printer drops them.
Member
Author
|
Let's give it a go 👍 |
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.
Fixes rectorphp/rector#9800
LogicalToBooleanRectorswapsand/orfor&&/||. The boolean operators bind tighter than the logical ones, so any operand whose top-level operator sits between them in precedence lost its parentheses on reprint — silently changing semantics.Affected operand types: ternary, elvis (
?:), compound assignments (+=,??=, ...),print,yield,yield from.Before (broken)
After (fixed)
(
(print "foo") && print "bar"is intentional — only the left operand needs wrapping; the right stays semantically identical.)Fix
BetterStandardPrinter::wrapBinaryOpWithBrackets()now reprints lower-precedence operands of a newly createdBooleanAnd/BooleanOrso the pretty-printer re-adds the parentheses.yield <value>additionally needs the parentheses forced, since the format-preserving printer drops them for that form.