Skip to content

Varifier false positive when the declared type is annotated #4698

@ben-manes

Description

@ben-manes

When enabling NullAway's jspecify mode, the use of arrays needs to be annotated for stricter nullability. For example,

.../LocalAsyncCache.java:818: warning: [NullAway] Writing @Nullable expression into array with @NonNull contents.
          oldValue[0] = Async.getIfReady(oldValueFuture);

To satisfy this check without a suppression requires the change

-var oldValue = (V[]) new Object[1];
+@Nullable V[] oldValue = (V[]) new Object[1];

ErrorProne then emits a warning,

.../LocalAsyncCache.java:800: warning: [Varifier] Consider using `var` here to avoid boilerplate.
      @Nullable V[] oldValue = (V[]) new Object[1];
                    ^
    (see https://errorprone.info/bugpattern/Varifier)
  Did you mean '@Nullable var oldValue = (V[]) new Object[1];'?

However that suggestion is invalid by JLS and instead the warning must be suppressed.

.../LocalAsyncCache.java:800: error: annotation interface not applicable to this kind of declaration
        @Nullable var oldValue = (V[]) new Object[1];
        ^
  1 error

Note that the annotation is not carried forward in the inferred type for NullAway to use if instead trying to resolve both checks

var oldValue = (@Nullable V[]) new Object[1];

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