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
RemoveUnusedImportstracks import usage, especially in generic type parameters. - Assistant
Identifying the issue: the check at lines 264–268 removes imports when the simple name isn't in
sourceIdentifierNames, but types used only in generic positions may be missing there while still present inunqualifiedTypeNames. Applying the fix. - Assistant
Fixed
RemoveUnusedImportsincorrectly dropping imports for types that appear only in generic type arguments (e.g.extends AtomicReference<BloomFilter<T>>).Cause
A guard added for #7333 removed non-static imports when the imported simple name was missing from
sourceIdentifierNames. Names in generic positions are often not collected there becausevisitTypeNamedoes not walkParameterizedTypetrees, while only looked at nodes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.