google / google/error-prone

Varifier false positive when the declared type is annotated

Open
#4,698 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

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

```console
.../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
```diff
-var oldValue = (V[]) new Object[1];
+@Nullable V[] oldValue = (V[]) new Object[1];
```

ErrorProne then emits a warning,
```console
.../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.

```console
.../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
```java
var oldValue = (@Nullable V[]) new Object[1];
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.