google / google/error-prone

False positive with `Var`

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

Description

### Description of the problem / feature request:

VarChecker seems to behave erratically on implemented classes (and some other cases I haven't been able to fully pin down).

### Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

```
public class HealthCheckManager implements io.dropwizard.lifecycle.Managed {

private final HealthCheckRegistry registry;
private final ImmutableMap checks;

@Inject
HealthCheckManager(HealthCheckRegistry registry, Map checks) {
this.registry = registry;
this.checks = ImmutableMap.copyOf(checks);
}

@Override
public void start() {
checks.forEach(registry::register);
}

@Override
public void stop() {}
}
```

Generates the following:

```
/HealthCheckManager.java:20: error: [Var] Non-constant variable missing @Var annotation
HealthCheckManager(HealthCheckRegistry registry, Map checks) {
^
(see https://errorprone.info/bugpattern/Var)
Did you mean 'HealthCheckManager(@Var HealthCheckRegistry registry, Map checks) {'?
/HealthCheckManager.java:20: error: [Var] Non-constant variable missing @Var annotation
HealthCheckManager(HealthCheckRegistry registry, Map checks) {
^
(see https://errorprone.info/bugpattern/Var)
Did you mean 'HealthCheckManager(HealthCheckRegistry registry, @Var Map checks) {'?
```

Emphasizing the state doesn't fix it, if I mark the two parameters in question `final` it compiles normally, but I get the following:

```/HealthCheckManager.java:20: error: [Var] Unnecessary 'final' modifier.
HealthCheckManager(final HealthCheckRegistry registry, final Map checks) {
^
(see https://errorprone.info/bugpattern/Var)
Did you mean 'HealthCheckManager(HealthCheckRegistry registry, final Map checks) {'?
/HealthCheckManager.java:20: error: [Var] Unnecessary 'final' modifier.
HealthCheckManager(final HealthCheckRegistry registry, final Map checks) {
^
(see https://errorprone.info/bugpattern/Var)
Did you mean 'HealthCheckManager(final HealthCheckRegistry registry, Map checks) {'?
```

Annotating them with `@Var` prevents the alert, but is inaccurate. Changing the names of the variables doesn't seem to help.

### What version of Error Prone are you using?

2.3.2

```
openjdk 11 2018-09-25
OpenJDK Runtime Environment AdoptOpenJDK (build 11+28)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11+28, mixed mode)```

### Have you found anything relevant by searching the web?

Could not find it in the github issues nor in `error-prone-discuss`.

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.