RemoveUnusedImports removes imports when used in Generics only <!-- Thank you for…
- User
RemoveUnusedImports removes imports when used in Generics only
What version of OpenRewrite are you using?
I am using
- OpenRewrite v8.31.1
- Maven/Gradle plugin v6.40.0
How are you running OpenRewrite?
I am using the Maven plugin, and my project is a single module project.
xml<plugin> <groupId>org.openrewrite.maven</groupId> <artifactId>rewrite-maven-plugin</artifactId> <version>6.40.0</version> <executions> <execution> <phase>process-sources</phase> <goals> <goal>run</goal> </goals> <configuration> <activeRecipes combine.children="append"> <recipe>org.openrewrite.java.RemoveUnusedImports</recipe> <recipe>org.openrewrite.java.OrderImports</recipe> </activeRecipes> </configuration> </execution> </executions> </plugin>What is the smallest, simplest way to reproduce the problem?
javaimport com.google.common.hash.BloomFilter; import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("UnstableApiUsage") public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> { }What did you expect to see?
javaimport com.google.common.hash.BloomFilter; import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("UnstableApiUsage") public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> { }What did you see instead?
java - Assistant
Investigating how
RemoveUnusedImportsdetects used imports and why generic-only usages are missed. - Assistant
Identifying the issue: line 227 marks imports unused when
combinedTypesis empty before checking whether the type name appears in source (e.g. in generics). Applying the fix. - Assistant
Improving package lookup:
qualid.getTarget().toString()may not match the package key used when indexing types. - Assistant
Reverting changes that broke wildcard import handling and excluding fully qualified Javadoc references from
sourceIdentifierNames. - Assistant
Summary
RemoveUnusedImportswas dropping imports for types that only appear in generic type arguments (e.g.BloomFilterinAtomicReference<BloomFilter<T>>). The logic added for #7333 treated “not intypesInUse/ unqualified type sets” as unused before checking whether the simple name still appears in source.Fix
In
RemoveUnusedImports.java:-
Keep imports when the simple name appears in source — On the early “empty
combinedTypes” path and the later type-matching path, do not mark an import unused ifsourceIdentifierNamesalready contains the imported type’s simple name (covers generic-only references likeBloomFilter).
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.