Repository navigation
Conversation
|
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>
4b7efe0 to
37fc792
Compare
|
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 |
ASTHelpers.getSymbol(Tree)returned null for aParenthesizedTreesuch as(x),(this.x), or the(lock)of asynchronizedstatement, which javac always parenthesizes. A check that read the null as "not a variable" silently skipped the operand, so every caller had to rememberstripParentheses; four checks in Error Prone did, and NullAway has added the same wrapper twice. javac'sTreeInfo.symbolskips the parentheses, and JLS §15.8.5 says a parenthesized expression denotes the variable its operand denotes.getSymbolnow returns the symbol of the expression inside the parentheses, andUnnecessaryBoxedVariable,CanIgnoreReturnValueSuggester,StaticGuardedByInstance, andSynchronizeOnNonFinalFieldcall it withoutstripParentheses.Three callers took a non-null
getSymbolas proof that the tree was a method invocation, and would have crashed on parentheses with the newgetSymbol. Method matchers built byMethodMatchersand byMethodInvocationMatcher.compilepass the tree togetReceiverType, which throwsIllegalArgumentException: Expected a JCFieldAccess or JCIdent from expression (Math.abs(-1)); Refaster's placeholder lookup casts a template'sreturn (placeholder(x));toMethodInvocationTreeand throwsClassCastException. 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_parenthesizedExpressionHasTheSymbolOfItsOperandcovers((x)),(x),(this.x), thesynchronized(lock), and(1 + 2), which still has no symbol.check_api/pom.xmlexcludesASTHelpersTestfrom the Maven build, so I ran it from a temporary copy incore: it fails without thegetSymbolchange.MethodMatchersTest#methodMatcher_doesNotMatchParenthesesAroundACall,MethodInvocationMatcherTest#compiledMatcher_doesNotMatchParenthesesAroundACall, andTemplateIntegrationTest#parenthesizedPlaceholderInReturnare 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,coreon JDK 25 passes.Fixes #6223
🤖 Generated with Claude Code