google / google/error-prone

RemoveUnusedImports false negative when the import is of an enum constant used as a switch target

Open
#4,000 0 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

Consider a Java file like this:

```java
import java.lang.annotation.ElementType;

import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;

class Dummy
{
public static void main(String[] args)
{
ElementType test = METHOD;
String result = switch (test)
{
case METHOD -> "m";
case FIELD -> "f";
default -> "o";
};
}
}
```

The Java file contains three static imports of an enum (`java.lang.annotation.ElementType`) constants: `CONSTRUCTOR`, `FIELD`, and `METHOD`. There is also a `switch` which has branches for `METHOD` and `FIELD`. The thing with switches is that the branch targets are "obvious" from the type of the value switched on, so they are not typical constant references and do not need to be neither fully qualified nor static-imported. So the static imports of `CONSTRUCTOR` and `FIELD` are not needed (`METHOD` is referenced directly in an assignment, so it's not unused). But the check reports only `CONSTRUCTOR`, which is neither referenced directly nor used as a branch target in the switch, and `FIELD` is a false negative.

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.