Skip to content

Make AssignmentExpression matcher parenthesis-invariant on nested assignments#5859

Open
lukman48 wants to merge 1 commit into
google:masterfrom
lukman48:fix/5827-assignmentexpression-parenthesis-invariant
Open

Make AssignmentExpression matcher parenthesis-invariant on nested assignments#5859
lukman48 wants to merge 1 commit into
google:masterfrom
lukman48:fix/5827-assignmentexpression-parenthesis-invariant

Conversation

@lukman48

Copy link
Copy Markdown

Description

Fixes #5827

The AssignmentExpression checker's matchAssignment method was not parenthesis-invariant. When an assignment expression was wrapped in ParenthesizedTree (e.g. int k = (a = b)), the checker did not look through the parentheses to determine the enclosing context. This caused the checker to miss the assignment, producing inconsistent results depending on whether parentheses were present.

Fix

  1. Check the C-ism exemption ((a = getFoo()) != null) before unwrapping parentheses, so it still applies correctly.
  2. Unwrap consecutive ParenthesizedTree nodes from the parent path before deciding whether the assignment is embedded in a larger expression.

Before/After behavior

Before: int k = (a = b) not flagged, int k = a = b flagged
After: Both forms flagged (paren-invariant)
C-ism if ((a = getObject()) != null) still exempted (unchanged)

Testing

  • All existing tests pass (6 tests)
  • Added parenthesizedAssignmentInDeclaration test covering the paren-wrapped case

When an assignment is wrapped in ParenthesizedTree (e.g. int k = (a = b)),
the checker previously did not look through the parentheses to determine
the enclosing context, causing inconsistent behavior between
int k = (a = b) and int k = a = b.

Fix: unwrap ParenthesizedTree nodes before examining the parent context.
The C-ism exemption ((a = getFoo()) != null) is checked before unwrapping
so it still applies correctly.

Fixes google#5827
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AssignmentExpression matcher is not parenthesis-invariant on nested assignments

1 participant