eclipse-jdt / eclipse-jdt/eclipse.jdt.core
organize imports adding pointless imports
- Dominant language
- Java
- Stars
- 237
- Forks
- 195
- Avg merge
- 1d 10h
- Merged PRs (30d)
- 49
Description
I have a Junit5 test that extends some base test class and therefore indirectly extends `org.assertj.core.api.Assertions`.
As a result, I can use `assertThat` methods directly without adding static imports.
However, when I edit the test in Eclipse my save actions trigger formatter and organize imports that adds this line:
```
import static org.assertj.core.api.Assertions.assertThat;
```
At the same time Eclipse renders a compiler warning saying `The import org.assertj.core.api.Assertions.assertThat is never used`.
Behavior like this renders the very cool feature of save-actions into something rather annoying.
I get the error when editing this file:
https://github.com/m-m-m/marshall/blob/d141e301031969f668153929b33b65823d071bd2/impl/protobuf/src/test/java/io/github/mmm/marshall/protobuf/ProtoBufFormatTest.java
I also tried to isolate the bug but was not lucky with a naive approach:
```
import org.assertj.core.api.Assertions;
public class ClassExtendingAssertions extends Assertions {
}
```
And now editing this file does NOT reproduce the bug as I would have expected:
```
import org.junit.jupiter.api.Test;
public class EclipseOrganizeImportsBug extends ClassExtendingAssertions {
@Test
public void test() {
assertThat(Boolean.TRUE).isTrue();
}
}
```
So its seems I am hitting an edge-case.
Tested with
```
Version: 2023-09 (4.29.0)
Build id: 20230907-1323
```
p.s.: IntelliJ actions on save feature is even worse in such case as you have no workaround. Eclipse is smart enough to save twice so invoking undo with `[ctrl][z]` will revert the modifications from save-actions. Also it seems that IntelliJ abandoned actions on save feature entirely in new versions. I love Eclipse save actions and in combination with devonfw-ide it is the excellent solution to ensure a consistent code-style even in large teams and to avoid diff-wars.
I am still a big fan of Eclipse and using it since IBM Visual Age for Java times (>20 years). Thanks for making Eclipse and consider this bug report.
I have always been pushing support for Eclipse as first class citizen in [devonfw-ide](https://github.com/devonfw/ide) and its successor [IDEasy](https://github.com/devonfw/IDEasy/).
Contributor guide
Research direction
Start by reproducing the save-action behavior in impl/protobuf/src/test/java/io/github/mmm/marshall/protobuf/ProtoBufFormatTest.java with Eclipse 2023-09 and organize imports enabled. Compare the inherited AssertJ usage with the added static import and investigate why the minimal ClassExtendingAssertions example does not reproduce it. Done means organizing imports no longer adds the unused assertThat import in this edge case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100