eclipse-jdt / eclipse-jdt/eclipse.jdt.core

Raise a warning on redundant imports (wildcard or module)

Open
#4,374 1 comment 0 reactions 1 assignee Claimed by @stephan-herrmann View on GitHub
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

This came up in the context of https://github.com/eclipse-jdt/eclipse.jdt.ui/pull/2436

Since Java 25 (or previous versions with preview flag) it is legal to write:
```java
import module java.base;
import java.util.List;
import java.util.function.*;
```

Given the first import, the other two are redundant.

We could compare this to a combination of regular imports and wildcard imports:
```java
import static Foo.*;
import static Foo.x;

class Bar {
String myX = x;
}
```

Currently this would report
* the import Foo.x is never used (pointing to the wildcard import)

If we add `String myY = y; ` where y is a static field in Foo, then no warning will be raised, because
* x will be resolved via regular import
* y will be resolved via wildcard import.

Technically, it is correct to flag the wildcard import (or module import) as unused, if a more specific import suffices. But when the wildcard or module import is also used, wouldn't it make sense to raise a warning against individual imports?

For comparison: how does Organize Imports handle these situations:
* Organize Imports does not look at existing wildcard imports, but counts all imports from the same container and compares this number to the configured threshold to either create all explicit imports or one wildcard import
* For modules, https://github.com/eclipse-jdt/eclipse.jdt.ui/pull/2436 is about to add behavior whereby an existing module import may cause regular imports to be removed, if they're covered by the module import.

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.