From 161c201cc415581d52800fa261e2b5966f00a285 Mon Sep 17 00:00:00 2001 From: Vladimir Sitnikov Date: Wed, 4 Nov 2026 05:48:54 -0800 Subject: [PATCH] Document that `ASTHelpers.getSymbol(Tree)` returns null for a parenthesized expression MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `ASTHelpers.getSymbol((x))` returns null where `getSymbol(x)` returns the symbol of `x`, and the Javadoc did not say so. A caller that forgets `stripParentheses` gets no symbol for a parenthesized operand, including the expression of every `synchronized` statement, and usually treats that as "not a variable" without any error. As discussed in #6223, the method keeps returning null: callers treat a non-null result as a sign that the tree itself is the identifier, member select, or invocation, and some cast it or pass it to `getReceiver`, which throws `IllegalStateException` when given a parenthesized expression. The Javadoc now states the null result and points at `stripParentheses(Tree)`. A new test in `ASTHelpersTest` pins it, with that reason in its comment, so that a change making `getSymbol` look through parentheses fails it. The old Javadoc also promised null when a compilation error left the symbol unresolved. Error Prone does not run checks on a compilation unit with errors, and an unresolved invocation, `new` expression, or member reference throws `IllegalArgumentException` from its typed overload instead of returning null. The comment now states what the code returns: null for an identifier or member select that javac left without a symbol. The inline comment in `getSymbol` names module names as one such case. `check_api/pom.xml` lists `ASTHelpersTest.java` in `testExcludes`, so the GitHub Maven build does not compile or run the new test. I compiled and ran `ASTHelpersTest` by hand on JDK 25.0.4: all 67 tests pass. With a `ParenthesizedTree` case added at the top of `getSymbol(Tree)`, the new test fails with `expected: null` / `but was : x`. `javadoc -Xdoclint:all` on `ASTHelpers.java` reports no errors; both `{@link}` targets resolve. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Fixes https://github.com/google/error-prone/pull/6295 COPYBARA_INTEGRATE_REVIEW=https://github.com/google/error-prone/pull/6295 from vlsi:claude/javadoc-getsymbol-improvement-ba25fb 4f5b2414d86f9d53421600a95b890fa98c560e7c PiperOrigin-RevId: 1013782394 --- .../google/errorprone/util/ASTHelpers.java | 10 ++++-- .../errorprone/util/ASTHelpersTest.java | 33 +++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java b/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java index 5cea9535883..c984c8240ae 100644 --- a/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java +++ b/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java @@ -232,9 +232,13 @@ public static boolean sameVariable(ExpressionTree expr1, ExpressionTree expr2) { } /** - * Gets the symbol for a tree. Returns null if this tree does not have a symbol because it is of - * the wrong type, if {@code tree} is null, or if the symbol cannot be found due to a compilation - * error. + * Gets the symbol that a tree declares or refers to. + * + *

Returns null if {@code tree} is null, if it is a kind of tree that has no symbol, or if + * javac attached no symbol to an identifier or member select, as for some module names. A {@link + * ParenthesizedTree} has no symbol, so {@code getSymbol((x))} returns null where {@code + * getSymbol(x)} returns the symbol of {@code x}. Call {@link #stripParentheses(Tree)} first where + * the parentheses do not matter. */ public static @Nullable Symbol getSymbol(Tree tree) { if (tree instanceof AnnotationTree annotationTree) { diff --git a/check_api/src/test/java/com/google/errorprone/util/ASTHelpersTest.java b/check_api/src/test/java/com/google/errorprone/util/ASTHelpersTest.java index 6c898fde4d5..16260a37335 100644 --- a/check_api/src/test/java/com/google/errorprone/util/ASTHelpersTest.java +++ b/check_api/src/test/java/com/google/errorprone/util/ASTHelpersTest.java @@ -76,6 +76,7 @@ import com.sun.source.tree.NewClassTree; import com.sun.source.tree.PackageTree; import com.sun.source.tree.ParameterizedTypeTree; +import com.sun.source.tree.ParenthesizedTree; import com.sun.source.tree.ReturnTree; import com.sun.source.tree.Tree; import com.sun.source.tree.TypeParameterTree; @@ -1483,6 +1484,38 @@ public Void visitMemberSelect(MemberSelectTree tree, VisitorState state) { assertCompiles(scanner); } + // Callers treat a non-null symbol as a sign that the tree itself is an identifier, member select, + // or invocation, and some cast it or pass it to getReceiver, which throws IllegalStateException + // when given a parenthesized expression. See https://github.com/google/error-prone/issues/6223. + @Test + public void getSymbol_parenthesizedExpressionHasNoSymbol() { + writeFile( + "Foo.java", + """ + class Foo { + int x; + + int f() { + return (x) + 1; + } + } + """); + + TestScanner scanner = + new TestScanner() { + @Override + public Void visitParenthesized(ParenthesizedTree tree, VisitorState state) { + assertThat(getSymbol(tree)).isNull(); + assertThat(String.valueOf(getSymbol(tree.getExpression()))).isEqualTo("x"); + setAssertionsComplete(); + return super.visitParenthesized(tree, state); + } + }; + tests.add(scanner); + + assertCompiles(scanner); + } + /** Replaces all throws clauses with more specific exceptions. */ @BugPattern( summary = "Replaces all throws clauses with more specific exceptions.",