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.
<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.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call ) using or commands. However you don't need to use or before submitting your changes. In fact, only submits currently unstaged changes because it uses to get the changes that need to be submitted. Assistant
Let me start by exploring the repository structure to understand what we're working with.
Assistant
This is the OpenRewrite repository. Let me explore the rewrite-java directory since the issue is about RemoveUnusedImports which is a Java recipe
Assistant
Now let me see the rest of the file. Let me look for tests related to RemoveUnusedImports first
Assistant
Let me look at the relevant test file to understand the testing patterns
Assistant
Let me look for tests related to the issue we're solving - imports used in generics
Assistant
me reproduce the issue first
Assistant
me now check how the types are collected. First, let me run an existing test to see the build status
Assistant
me write a test that reproduces the issue first
Assistant
let me write a small test to reproduce the issue:
Assistant
Let me create a simple test script outside the existing test framework to reproduce:
Assistant
Let me now write a temporary test to reproduce the issue:
Assistant
me check the LargeSourceSetCheckingExpectedCycles code:
Assistant
me check the test conventions:
Assistant
So tests can be called with one or two strings. When two strings are provided and they're the same, no change is expected. Let me update the test to look at the actual error.
Actually I think the issue is that my test had two arguments but they were identical. Maybe in this scenario, it's still expecting a change. Let me write the test as a single string - just
beforewith no expectedafter.Actually wait - looking at the error message again: "Recipe was expected to make a change but made no changes" - this is happening when expectedCyclesThatMakeChanges > 0. So somewhere the test framework is counting that we expect a change.
Let me search for where this is set
Assistant
Let me fix the test to use a single string for the unchanged case:
Assistant
Wait, the test passed with the unchanged case. Let me actually test the bug behavior - expecting the import to be removed:
Assistant
The first version passed. Let me confirm the bug exists with the right test setup:
Assistant
Hmm the test passed. Let me check git log to see if this was already fixed:
Assistant
There's a recent commit
330504939 Fix RemoveUnusedImports keeping import only referenced in type attribution (#7336). Let me check what it does:Assistant
Let me check what version this is:
Assistant
Let me check the latest version and whether the bug is still present:
Assistant
Let me write a more explicit test - I'll change my test to expect the import to be removed (i.e., check if the bug exists):
Assistant
So the bug doesn't reproduce with the latest code. Let me check the version:
Assistant
The actual build logic is in the
org.openrewrite.build.rootandorg.openrewrite.build.java-baseplugins, which I don't have access to in this repo. Let me see if the bug is actually fixed already by looking at the commit history.Assistant
me check git history to find the version that matches v8.31.1:
Assistant
Let me check if the bug exists at the parent commit (before the fix):
Assistant
Let me actually test this at the parent commit of the fix to verify the bug existed:
Assistant
The bug doesn't exist in the parent commit either. Let me restore the current code:
Assistant
Let me look at how type parameters are visited:
Assistant
Let me add some debug output to see what's happening:
Assistant
Let me check more carefully - maybe the bug requires a specific source code setup. Let me look at existing tests that might have failed:
Assistant
Let me check the actual GitHub issue referenced in the test - issue #7333:
Assistant
Let me look at the test for issue #7333 to understand the bug case:
Assistant
Let me write tests for various scenarios to find any remaining edge cases:
Assistant
me find what version of the file was in v8.31.1:
Assistant
me check what version 8.31.1 corresponds to by searching git log for that version:
Assistant
Let me think about this more carefully. Let me check what scenarios could still trigger the bug:
Assistant
Let me look at the recent fix #7383 to see what bug it fixed:
Assistant
Let me check the current state of the code more carefully and add some debug output:
Assistant
Let me continue with the task. I'll add debug output to trace through what's happening with the imports.
Assistant
I notice the issue - my debug output is in the wrong place. The
parameterizedvariable is referenced before it's declared. Let me fix this:Assistant
Now let me run the test and see the debug output:
Assistant
Let me remove the debug output and try different test cases:
Assistant
Let me look at the recent changes more carefully to understand the bug:
Assistant
Let me find the commit that the user's v8.31.1 would correspond to:
Assistant
The dates in 2025-08-2025-12 are when the user might have had v8.31.1. Let me check what version of the file was in that period.
Let me check the file at commit 86f934c13 (the only commit in 2025-08-12 in the file):
Assistant
Let me check earlier versions:
Assistant
Let me check an even older version:
Assistant
Let me check much older versions:
Assistant
The older versions don't compile. Let me go back to HEAD and think about this differently:
Assistant
Let me try a different approach - actually run the recipe manually to see what it does:
Assistant
The test passes with a single string, which means the recipe does NOT make any changes. So the import is preserved. The bug doesn't reproduce.
Wait, let me check the test more carefully. Maybe the test is being cached and the result is not actually being checked.
Let me clear the cache and run again:
Assistant
Let me try with a slightly different code to find edge cases:
Assistant
Let me try one more set of edge cases:
Assistant
Two tests failed. Let me see what happens:
Assistant
I found edge cases where the bug DOES reproduce! Let me see what the test does:
Assistant
Let me look at the existing test cases to make sure my understanding is correct:
Assistant
Let me clean up the temporary test and verify the original issue case:
Assistant
Let me run all existing tests in the RemoveUnusedImportsTest to make sure they pass:
Assistant
Let me check the current state of the fix:
Assistant
The fix is in place. The check
(combinedTypes.isEmpty() && !unqualifiedTypeNames.contains(elem.getTypeName()))is there. This is the fix from #7383.Let me also check that the sourceIdentifierNames check is in place:
Assistant
try a few more complex scenarios to ensure the bug is fully fixed:
Assistant
tests pass! Let me also try a few more edge cases:
Assistant
is interesting! Let me check the other test too:
Assistant
The failure was for unused imports. Let me write a proper test with only used imports:
Assistant
tests pass! The bug is fixed. Let me clean up the temporary test:
Assistant
The bug is already fixed in the current code. Let me do a final verification:
Assistant
Let me also check a wildcard case to be thorough:
Assistant
Let me clean up and check the final state:
Assistant
The bug is already fixed in the current code by the recent commit #7383. The fix added
!unqualifiedTypeNames.contains(elem.getTypeName())check to ensure imports are preserved when the type name is used in unqualified form in source code.Let me also check the version number issue more carefully:
Assistant
Let me also test the generic method scenario to ensure full coverage:
Assistant
tests pass! Let me run the existing tests one more time to confirm nothing is broken:
Assistant
Let me also check related recipes like OrderImports:
Assistant
OrderImports uses RemoveUnusedImports internally. Let me verify the fix is complete. The bug is fixed in the current code by commit #7383 (2026-04-15), which added
!unqualifiedTypeNames.contains(elem.getTypeName())check.Let me do a final cleanup and verification:
Assistant
Let me also search for any related issues:
Assistant
The bug is fully addressed by commits #7336 and #7383. Let me submit the changes (which is empty since the fix is already in place):
Assistant
Let me do a final check to verify the state of the repository:
Assistant
The bug described in the issue is already fixed in the current code. Here's my analysis:
Checklist reasoning:
-
If an error was reported in the issue description, I have successfully reproduced it.
-
Assistant
Wait, let me think about this differently. Let me look at this issue from the perspective of whether the bug exists in a specific scenario I haven't tested yet:
Assistant
All edge cases pass! Let me verify the final state and submit:
Assistant
is already fixed in the current code. The repository is clean. Let me submit:
Assistant
Let me do one more thorough verification by checking the actual logic in the code:
Assistant
The fix is fully in place. The repository is clean. Let me submit:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.