Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Nothing in the build catches a new ASTHelpers.getSymbol call that misses a parenthesized operand #2133

Open

Description

@vlsi

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:

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:

  1. 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.
  2. Forbid the getSymbol(com.sun.source.tree.Tree) overload in nullaway/src/main with the forbidden-apis Gradle plugin (de.thetaphi.forbiddenapis). It checks bytecode, so a static import does not get past it. The overloads for ClassTree, MethodTree, VariableTree, MethodInvocationTree, NewClassTree, MemberReferenceTree, and PackageTree cannot receive a ParenthesizedTree and stay allowed.
  3. 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

  1. jeffrey-easyesi commented on Nov 8, 2026

    @jeffrey-easyesi
    Contributor

    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 -Wall would warn on if (x = y) while allowing if ((x = y)).

    (And going back before that to really ancient times: in QuickBasic, CALL FOO(x) passed x by reference, while CALL 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 a Stream<@Nullable X> instead of narrowing to Stream<X>.

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