Skip to content

Make ASTHelpers.getSymbol look through parentheses - #6224

Closed
vlsi wants to merge 1 commit into
google:masterfrom
vlsi:claude/getsymbol-stripparentheses-refactor-321133
Closed

vlsi wants to merge 1 commit into
google:masterfrom
vlsi:claude/getsymbol-stripparentheses-refactor-321133

Conversation

@vlsi

@vlsi vlsi commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

ASTHelpers.getSymbol(Tree) returned null for a ParenthesizedTree such as (x), (this.x), or the (lock) of a synchronized statement, which javac always parenthesizes. A check that read the null as "not a variable" silently skipped the operand, so every caller had to remember stripParentheses; four checks in Error Prone did, and NullAway has added the same wrapper twice. javac's TreeInfo.symbol skips the parentheses, and JLS §15.8.5 says a parenthesized expression denotes the variable its operand denotes.

getSymbol now returns the symbol of the expression inside the parentheses, and UnnecessaryBoxedVariable, CanIgnoreReturnValueSuggester, StaticGuardedByInstance, and SynchronizeOnNonFinalField call it without stripParentheses.

Three callers took a non-null getSymbol as proof that the tree was a method invocation, and would have crashed on parentheses with the new getSymbol. Method matchers built by MethodMatchers and by MethodInvocationMatcher.compile pass the tree to getReceiverType, which throws IllegalArgumentException: Expected a JCFieldAccess or JCIdent from expression (Math.abs(-1)); Refaster's placeholder lookup casts a template's return (placeholder(x)); to MethodInvocationTree and throws ClassCastException. These three now reject a parenthesized tree, so a method matcher still matches the call and not the parentheses around it, as before. Making method matchers match through parentheses would change which trees every existing matcher accepts, so it is left out of this change.

Compatibility for checker authors: getSymbol(Tree) on a parenthesized expression returns a symbol where it used to return null. A third-party checker that takes a non-null result as proof of a tree kind and then casts the tree needs the same kind check the three callers above got.

Verification

  • ASTHelpersTest#getSymbol_parenthesizedExpressionHasTheSymbolOfItsOperand covers ((x)), (x), (this.x), the synchronized (lock), and (1 + 2), which still has no symbol. check_api/pom.xml excludes ASTHelpersTest from the Maven build, so I ran it from a temporary copy in core: it fails without the getSymbol change.
  • MethodMatchersTest#methodMatcher_doesNotMatchParenthesesAroundACall, MethodInvocationMatcherTest#compiledMatcher_doesNotMatchParenthesesAroundACall, and TemplateIntegrationTest#parenthesizedPlaceholderInReturn are new; each fails with the exception quoted above when its guard is removed. The two matcher tests also check that the same matcher still flags the call itself.
  • mvn test -pl check_api,core on JDK 25 passes.

Fixes #6223

🤖 Generated with Claude Code

@google-cla

google-cla Bot commented Oct 21, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

ASTHelpers.getSymbol(Tree) returned null for a ParenthesizedTree such as `(x)`, `(this.x)`, or the `(lock)` of a synchronized statement, which javac always parenthesizes. A check that read the null as "not a variable" silently skipped the operand, so every caller had to remember stripParentheses; four checks in Error Prone did. javac's TreeInfo.symbol skips the parentheses, and JLS 15.8.5 says a parenthesized expression denotes the variable its operand denotes.

getSymbol now returns the symbol of the expression inside the parentheses, and UnnecessaryBoxedVariable, CanIgnoreReturnValueSuggester, StaticGuardedByInstance, and SynchronizeOnNonFinalField call it without stripParentheses.

Three callers took a non-null getSymbol as proof that the tree was a method invocation. Method matchers built by MethodMatchers and by MethodInvocationMatcher.compile pass the tree to getReceiverType, which threw `IllegalArgumentException: Expected a JCFieldAccess or JCIdent from expression (Math.abs(-1))` for `(Math.abs(-1))`; Refaster's placeholder lookup cast a template's `return (placeholder(x));` to MethodInvocationTree and threw ClassCastException. These three now reject a parenthesized tree, so a method matcher still matches the call and not the parentheses around it.

Fixes google#6223

Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
@cushon

cushon commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

Thanks for investigating this. I replied in #6223 (comment), I think this is a nice idea to handle parenthesis in a general way, but in practice I'm concerned about the compatibility impact for checks that use getSymbol as a guard before casting to a specific AST node, and I think most of the issues we've seen with checks not handling syntactic variations around parenthesis are low priority.

@cushon cushon closed this Nov 4, 2026
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.

ASTHelpers.getSymbol returns null for a parenthesized expression, while javac's TreeInfo.symbol returns the symbol inside

2 participants