Description
ASTHelpers.getSymbol returns null for a ParenthesizedTree, so a NullAway check that resolves the symbol of an operand the user wrote stops seeing it once the user adds redundant parentheses. The metamorphic oracle of #2009 found two such call sites, and each got its own fix:
- Strip parentheses before the futures and gRPC handlers match an argument #2010:
GrpcHandlerreportedpassing @Nullable parameter 'h.get(K)' where @NonNull is requiredforif (h.containsKey((K))) { take(h.get(K)); }. - Strip parentheses before handlers match stream callbacks and boolean literals #2002:
StreamNullabilityPropagatordid not treatfilter((Objects::nonNull))as null-rejecting in JSpecify mode.
Both sites now call ASTHelpers.getSymbol(ASTHelpers.stripParentheses(...)). The other 138 ASTHelpers.getSymbol calls in nullaway/src/main/java pass the tree as it is. Many of them get their tree from a dataflow node or a declaration, which carries no parentheses, but nothing records which ones, and nothing makes the author of a new call decide.
Error Prone declined to make getSymbol look through parentheses (google/error-prone#6223 (comment)). Its reason holds inside NullAway too: code that reads getSymbol(tree) != null as proof that tree is an identifier, a member select, or an invocation then casts tree or passes it to ASTHelpers.getReceiver, which throws IllegalStateException for a ParenthesizedTree. So stripping parentheses inside one shared helper for every caller is not safe either; each call site needs a decision.
Acceptance criterion
A call to ASTHelpers.getSymbol(Tree) added to nullaway/src/main fails the build unless the author marks it, and the failure message names the helper to use instead.
One possible shape
This is a sketch, and the mechanism is yours to choose:
- Add two helpers to
NullabilityUtil. One strips parentheses and returns the symbol of the stripped expression; its Javadoc says that a caller inspects the stripped tree and not the original. The other resolves the tree as written, for trees that come from dataflow nodes or declarations. - Forbid the
getSymbol(com.sun.source.tree.Tree)overload innullaway/src/mainwith the forbidden-apis Gradle plugin (de.thetaphi.forbiddenapis). It checks bytecode, so a static import does not get past it. The overloads forClassTree,MethodTree,VariableTree,MethodInvocationTree,NewClassTree,MemberReferenceTree, andPackageTreecannot receive aParenthesizedTreeand stay allowed. - Move the existing calls to one helper or the other one call site at a time, not with search and replace, because of the hazard above.
This adds a build plugin and touches 31 files. A custom Error Prone check in buildWithNullAway would do the same job without a new plugin, at the cost of writing and testing the check.
Out of scope
#2002 also fixed handlers that tested instanceof on the tree as written, such as a parenthesized lambda passed to filter. Neither a forbidden-API rule nor a helper catches that pattern, so the #2009 oracle remains the check for it.
I can send the pull request if this direction works for you.
Activity
jeffrey-easyesi commentedon Nov 8, 2026
I'd call this a feature, not a bug! Extra parentheses are often used to intentionally hide an expression from getting special treatment.
see e.g. https://errorprone.info/bugpattern/LogicalAssignment, which follows a very old tradition: I remember even back in the '90s,
gcc -Wallwould warn onif (x = y)while allowingif ((x = y)).(And going back before that to really ancient times: in QuickBasic,
CALL FOO(x)passed x by reference, whileCALL FOO((x))made a copy.)So, I'd think if someone's writing something like
containsKey((key))it's because they don't want analyzers making assumptions about the key.I could actually imagine
.filter((Objects::nonNull))being useful if you specifically need to have aStream<@Nullable X>instead of narrowing toStream<X>.