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.
<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?
import 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?
import 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?
import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("UnstableApiUsage") public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> { }What is the full stack trace of any errors you encountered?
stacktrace output hereAre you interested in contributing a fix to OpenRewrite?
Interface
No new interfaces are introduced.
- Assistant
Investigating how
RemoveUnusedImportsdetects import usage, focusing on generic type parameters. - Assistant
Identifying the issue: the
sourceIdentifierNamescheck doesn't account for types referenced only viavisitTypeName(e.g. nested generic arguments). EnhancingcollectSourceIdentifierNamesto recurse throughvisitTypeNameon parameterized types. - Assistant
Identifying the root cause: imports used only in generics can be removed at lines 227 and 269 when
combinedTypesandunqualifiedTypeNamesare empty, even if the simple name appears in source. Updating those conditions. - Assistant
Fixing
typesByPackageto recursively include nested generic type arguments socombinedTypesmatches imports used only in generics. - Assistant
Fixed
RemoveUnusedImportsincorrectly dropping imports for types that only appear in generic positions (e.g.extends AtomicReference<BloomFilter<T>>).Cause
After the static-import parameter-type fix (#7333), the recipe treated imports as unused when they weren’t in
combinedTypes/unqualifiedTypeNames, even when the simple name was still present in source (typical for generic type arguments). Nested generic arguments also weren’t always folded into the type-usage map.Changes (
RemoveUnusedImports.java)collectSourceIdentifierNames– now records type names outside imports and walks nested arguments (so names like in count as used).
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.