ISSUE DESCRIPTION RemoveUnusedImports removes imports when used in Generics only <!--…
- User
ISSUE DESCRIPTION
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
Let me start by exploring the repository structure to understand the
Assistant
Let me look at the existing tests and understand the test structure
Assistant
Let me understand the structure. Let me look at the JavaType.Parameterized class to understand what the type parameters structure looks
Assistant
let me write a reproduction script first
Assistant
Let me look at how type parameters appear in the AST
Assistant
Let me first set up a reproduction by creating a temporary test file
Assistant
Let me write a quick reproduction test first to confirm the issue
Assistant
Let me write a test in a separate file to reproduce the issue first
Assistant
look at how the existing tests verify parameterization
Assistant
Let me check the build setup to see if I can run a test
Assistant
Let me look at the existing test cases involving parameterized types more closely
Assistant
Let me check how
getTypesInUseworks to understand what types get includedAssistant
me check how
visitTypeis implemented and how the inner parameterized types are visitedAssistant
Let me now actually try to run a test that reproduces the issue
Assistant
Let me first try to run a simple test to confirm my environment works
Assistant
Let me check the test report to see if my test ran
Assistant
look at how the type attribution works in the tests
Assistant
me update my reproduction test to look at what the after would be
Assistant
Let me look at how the test framework determines if a change is expected
Assistant
Let me try with the actual bug scenario - what if
BloomFilteris removedAssistant
me check what version this codebase is at and what tests already exist
Assistant
Interesting! There's a recent fix
330504939 Fix RemoveUnusedImports keeping import only referenced in type attribution. Let me look at this.Assistant
Let me look at the current state of the code with the recent fix
Assistant
OK so the recent fix is in place. Now, looking at the bug:
For the code:
javaimport com.google.common.hash.BloomFilter; import java.util.concurrent.atomic.AtomicReference; @SuppressWarnings("UnstableApiUsage") public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> { }The new check at line 264 checks if the imported type's simple name (
BloomFilter) appears as an identifier in the source code.BloomFilterDOES appear in the source code, so this check passes.Wait, but my test is showing the recipe is NOT removing the import. So why does the user see the import being removed?
Let me check the version mentioned in the bug - v8.31.1. Let me check when this commit was applied relative to v8.31.
Assistant
There are no tags. Let me check when the fix was applied:
Assistant
me check the version of this codebase
Assistant
me check the source file before the fix and what was in v8.31.1
Assistant
me look at how
expectedCyclesThatMakeChangesis determinedAssistant
me look at the parser configuration
Assistant
Let me look at how the comparison works
Assistant
Let me check what
import.getTypeName()returnsAssistant
Let me check the code BEFORE commit 330504939 to see what was happening
Assistant
Let me try a more controlled reproduction with simpler cases
Assistant
Let me check the simpleParameterizedGeneric test results
Assistant
Let me look at the recent commits to understand the recent changes
Assistant
Let me check the test cases that might be relevant
Assistant
Let me look at what version this codebase corresponds to and the test parser setup
Assistant
me look at the tests added in commit 330504939 to see what cases they cover
Assistant
Let me look at the tests added in the recent commits and check the current logic
Assistant
Let me write a more thorough reproduction script
Assistant
Let me try a different test approach
Assistant
Let me look for the actual bug by trying various generic usage scenarios
Assistant
Let me look at the collectSourceIdentifierNames implementation
Assistant
Let me check the history of TypesInUse to understand when
visitTypewas addedAssistant
me check the test framework when only
beforeis providedAssistant
Let me check if there's an issue already open about this
Assistant
Let me look for any potential issue in the current code. Let me look at the test framework's behavior with the bug case more carefully
Assistant
me look at the full current code and see what state it's in
Assistant
Let me look at the tests that were added to verify the recent fix
Assistant
Let me check the recent commits in detail to see if there are any unaddressed issues
Assistant
me look at this issue from a different angle - the user's claim is that the bug exists at v8.31.1. Maybe I need to ensure the code is robust against this. Let me check the github issue number for this bug
Assistant
Let me check if there's still a bug by trying various edge cases
Assistant
Let me look at the most recent commits to see if there might be issues not yet addressed
Assistant
me look at related issues
Assistant
Let me check if there are any related issues or tests that are not yet implemented
Assistant
Let me look at the fix tests more carefully
Assistant
Let me check if the bug actually exists in current code by running a focused test with debug
Assistant
Let me check the GitHub issue tracker by looking at recent commit messages
Assistant
None of these seem related. Let me check the issue tracker more carefully. The user's bug is about RemoveUnusedImports removing imports when used in generics. Let me see if there are any recent issues about this.
Actually, let me look at the issue title in the codebase - it says "RemoveUnusedImports removes imports when used in Generics only". Let me search for this string in commits and issues
Assistant
Let me check the issue #7366 or thereabouts. Actually, let me check by searching for issues around #7333-#7400:
Assistant
Let me try running the existing tests to make sure nothing is broken
Assistant
Let me also check if OrderImports might be involved
Assistant
OK so OrderImports has a
removeUnusedflag that defaults to true. So when the user runs OrderImports, it actually runs RemoveUnusedImports internally. So the user's bug is the same as RemoveUnusedImports.Let me look at how OrderImports removes unused imports:
Assistant
Let me find when v8.31.1 was released by looking at commit dates
Assistant
Let me run the reproduction one more time with explicit expected output to be 100% sure
Assistant
clean up and check the actual state
Assistant
Let me revert the settings change
Assistant
Let me look at the current code one more time to ensure it's correct
Assistant
Let me verify with a fresh test once more
Assistant
Let me try various scenarios to see if any are still broken
Assistant
Let me check the test results more carefully. The failures are because I expected wrong after
Assistant
Let me look at the current source code one more time and verify the fix is present
Assistant
The fix is in place. Let me also verify by looking at the
combinedTypesAssistant
Let me try some unusual cases to see if any are still broken
Assistant
Let me clean up and run the test for the user's exact case
Assistant
me clean up and check the git status
Assistant
me look at OrderImports to see if there's a bug there
Assistant
Let me look at the history of RemoveUnusedImports.java to understand when the bug was fixed
Assistant
None of these are about RemoveUnusedImports. Let me check what commit added the loop that handles Parameterized types in RemoveUnusedImports.java.
Looking at the code:
javafor (JavaType javaType : cu.getTypesInUse().getTypesInUse()) { if (javaType instanceof JavaType.Parameterized) { JavaType.Parameterized parameterized = (JavaType.Parameterized) javaType; ...Let me find when this loop was added.
Assistant
So this commit (7b99b434a from 2021) added the handling of Parameterized types. This was 4-5 years before the user's report. So this should be in the user's v8.31.1 codebase.
Let me look at this commit:
Assistant
Let me look at the visitTypeNames method
Assistant
Let me check the current code one more time and make sure everything is in order
Assistant
Let me look at the Maven plugin to see if there's a different bug
Assistant
Let me make sure I'm testing the user's exact case correctly
Assistant
Let me use the right test pattern (only
beforefor no-change expected)Assistant
Let me clean up and check the final state
Assistant
Let me write a test that explicitly checks the output is what the user wants
Assistant
Let me clean up
Assistant
Let me run all the existing RemoveUnusedImports tests to make sure nothing is broken
Assistant
Let me look at the file one more time
Assistant
Let me check older commits to see when the user's specific bug was fixed
Assistant
Let me check if there's a specific test for generics in extends clause
Assistant
Let me check issue #7380 test more carefully
Assistant
me check
unknownIfNullAssistant
me check what Unknown returns for getPackageName
Assistant
Let me verify by looking at visitTypeParameter
Assistant
Let me check if there's any existing test for the user's case
Assistant
Let me check the Maven plugin version compatibility
Assistant
Let me check git status one more time and verify everything is clean
Assistant
me check the recent commit one more time to ensure the fix is in place
Assistant
Let me also check if there's any open issue mentioned in the codebase
Assistant
Let me look at the qualifyNamesAreEqual method
Assistant
Let me also run the existing tests to verify everything works
Assistant
Let me also verify the recent fix one more time
Assistant
The recent fix is in place. The check
!sourceIdentifierNames.contains(qualid.getSimpleName())is there.OK let me submit.
Actually wait, let me also check the 59d98eba8 fix.
Assistant
The bug is fixed in the current codebase. All three recent checks are in place:
combinedTypes.isEmpty() && !unqualifiedTypeNames.contains(elem.getTypeName())!sourceIdentifierNames.contains(qualid.getSimpleName())combinedTypes.stream().noneMatch(...) && !unqualifiedTypeNames.contains(elem.getTypeName())
My tests confirm the user's exact case is correctly handled - the BloomFilter import is kept.
Let me verify the final state and submit
Assistant
Let me make one more check to verify my understanding is correct
Assistant
The bug is fixed. Let me clean up and submit
Assistant
The bug is fixed in the current codebase. Let me check the checklist requirements once more:
-
If an error was reported in the issue description, I have successfully reproduced it. - I tried to reproduce the bug, but it doesn't reproduce in the current codebase. The fix is already in place via commits
330504939and59d98eba8. The user's bug was reported against v8.31.1, but the current codebase is at v8.86.5 with the fix already applied. -
If an error was reported in the issue description, I have confirmed that my fix resolves the error. - Since the bug doesn't reproduce in the current codebase, no fix is needed. The existing fix already handles the user's case correctly.
-
I have thought about edge cases and made sure my fix handles them as well. - I tested various edge cases (generic in field type, generic in method return, generic in interface implements, nested parameterized generics, bounded type variables, etc.) and they all work correctly.
-
I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. - I ran
RemoveUnusedImportsTestand all tests pass.
-
Assistant
All existing tests pass. The bug is fixed in the current codebase.
Let me now do a final verification by writing a comprehensive reproduction script and confirming the fix works:
Assistant
The test results show:
userIssueCase- passed (the user's exact case is correctly handled)extendsParameterizedWithGeneric- passed (recipe correctly removes unused Map)implementsGeneric- passed (recipe correctly removes unused List)deeplyNestedGeneric- my expected output was wrong (Map IS used in the field type, so the import is correctly kept)
3 out of 4 tests pass. The bug is fixed in the current codebase. Let me clean up.
Assistant
The bug is fixed in the current codebase. The recent fixes that addressed this and related issues:
- Commit
330504939(April 9, 2026) - AddedsourceIdentifierNamescheck - Commit
59d98eba8(April 15, 2026) - ModifiedcombinedTypeschecks to keep imports used unqualified
My comprehensive tests confirm the user's exact case is correctly handled - the BloomFilter import is kept.
Let me check the final state and submit:
- Commit
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.