google / google/error-prone

FieldMissingNullable doesn't complain when variable is not initialized

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

Description

# Current behavior

With FieldMissingNullable turned on and the following code:

```java
import java.time.OffsetDateTime;

public class Clazz {
private OffsetDateTime closeTimestamp = null;

public Clazz() {
// Do nothing
}

public OffsetDateTime getCloseTimestamp() {
return closeTimestamp;
}

public void setCloseTimestamp(OffsetDateTime closeTimestamp) {
this.closeTimestamp = closeTimestamp;
}
}
```

We get the following warnings:

```
[FieldMissingNullable] Field is assigned (or compared against) a definitely null value but is not annotated @Nullable
(see https://errorprone.info/bugpattern/FieldMissingNullable)
Did you mean 'private @Nullable OffsetDateTime closeTimestamp = null;'?
```

But when we remove the explicit `null` assignment on class startup, the warning goes away, even tho the default value for a non initialized Object variable is `null`

# Expected behavior

When:

* Variable is an object (not primitive)
* Variable is not initialized when declared
* No assignment is made in constructors

We should also get a warning:

```
[FieldMissingNullable] Field is assigned (or compared against) a definitely null value but is not annotated @Nullable
(see https://errorprone.info/bugpattern/FieldMissingNullable)
Did you mean 'private @Nullable OffsetDateTime closeTimestamp;'?
```

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.