Skip to content

AssignmentExpression reports inconsistent findings when redundant parentheses are added or removed #6218

Closed

Description

@Chordrain

Error Prone version

Error Prone 2.49.0 (javac 21.0.8, --source/--target 21)

Affected checks

AssignmentExpression

Description

AssignmentExpression produces different findings when redundant parentheses are added to or removed from an inner assignment. For example, a = b = 0 is not flagged, while a = (b = 0) is flagged. Similarly, result = (bresult = new byte[len]) is flagged, while result = bresult = new byte[len] is not. The reproducer also includes unchanged findings and non-findings to show the result at each assignment site.

Per JLS §15.8.5, parentheses around an expression do not change its value, type, or constant-expression status. Adding or removing these parentheses should therefore not change the check's findings at the corresponding sites.

I verified the following pair on Error Prone 2.49.0:

Check Rewrite BEFORE AFTER Effect
AssignmentExpression a = b = 0 ↔ a = (b = 0) and the related sites below 2 3 verdict drift as parentheses are added/removed

The matcher appears to depend on ParenthesizedTree nodes around assignments. A possible fix is to account for redundant parentheses when inspecting the assignment expression and its enclosing context, for example by using ASTHelpers.stripParentheses where appropriate.

Reproducers

AssignmentExpression — verdict is not parenthesis-invariant

The check flags an assignment used inside a larger expression, but the result depends on redundant parentheses around the inner assignment — in both directions: a = b = 0 is not flagged while a = (b = 0) is, whereas result = (bresult = new byte[len]) is flagged while result = bresult = new byte[len] is not.

// BEFORE — 2 findings (on `int i = j = 0;` and `result = (bresult = ...)`)
public class Sample {
  int a, b, j, l;
  byte[] bresult;
  void m1() { a = b = 0; }                        // NOT flagged
  void m2() { if ((a = b = 0) != 1) {} }          // NOT flagged
  void m3() { int i = j = 0; }                    // flagged
  void m4(int len) {
    int k = (l += 1);                             // NOT flagged
    byte[] result;
    result = (bresult = new byte[len]);           // flagged
  }
}
// AFTER — 3 findings; per-site verdicts flip as parentheses are added/removed
public class Sample {
  int a, b, j, l;
  byte[] bresult;
  void m1() { a = (b = 0); }                      // now flagged
  void m2() { if ((a = (b = 0)) != 1) {} }        // now flagged
  void m3() { int i = (j = 0); }                  // flagged
  void m4(int len) {
    int k = l += 1;                               // NOT flagged
    byte[] result;
    result = bresult = new byte[len];             // now NOT flagged
  }
}

Expected behavior

The BEFORE and AFTER versions should produce the same findings at each corresponding assignment site. Adding or removing redundant parentheses around an inner assignment should not change whether that site is reported.

Actual behavior

The BEFORE version produces 2 findings, while the AFTER version produces 3. The findings change at individual sites as follows:

Site BEFORE AFTER
m1: a = b = 0 → a = (b = 0) Not flagged Flagged
m2: if ((a = b = 0) != 1) → if ((a = (b = 0)) != 1) Not flagged Flagged
m3: int i = j = 0 → int i = (j = 0) Flagged Flagged
m4: int k = (l += 1) → int k = l += 1 Not flagged Not flagged
m4: result = (bresult = new byte[len]) → result = bresult = new byte[len] Flagged Not flagged

The overall count hides changes in both directions: two sites gain findings when parentheses are added, while one site loses its finding when parentheses are removed.

Activity

  1. cushon commented on Nov 4, 2026

    @cushon
    Collaborator

    Thanks for the report. AssignmentExpression only exempts direct chained assignments like x = y = 0 and single-parenthesized binary condition idioms like if ((x = foo()) != null), and we don't plan to expand that exemption to arbitrarily parenthesized assignments (see https://errorprone.info/docs/criteria#false-positives-false-negatives-and-bug-reports).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions