Skip to content
TheChatArchiveThe Chat Archive
DeepSeekdeepseek/deepseek-v4-pro· deepseekHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

ISSUE DESCRIPTION RemoveUnusedImports removes imports when used in Generics only <!--…

  1. 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 here
    

    Are you interested in contributing a fix to OpenRewrite?

    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 using git reset --hard command. Additionally, you can use git stash and git stash pop to 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 submit) using git status or git diff commands. However you don't need to use git add or git commit before submitting your changes. In fact, submit only submits currently unstaged changes because it uses git diff to get the changes that need to be submitted.

    CHECKLIST

    Before submitting your solution, please go over the following checklist and make sure you've done everything:

    • If an error was reported in the issue description, I have successfully reproduced it.
    • If an error was reported in the issue description, I have confirmed that my fix resolves the error.
    • I have thought about edge cases and made sure my fix handles them as well.
    • I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.

    Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!

    (Current directory: /rewrite, current file: none) bash-$

  2. DeepSeek Assistant

    Let me start by understanding the repository structure and the issue. The issue is about RemoveUnusedImports removing imports when they are used in Generics only. Specifically, in code like:

    import com.google.common.hash.BloomFilter;
    import java.util.concurrent.atomic.AtomicReference;
    
    @SuppressWarnings("UnstableApiUsage")
    public class BloomFilterRef<T> extends AtomicReference<BloomFilter<T>> {
    }
    

    The BloomFilter import is being removed even though it's used in AtomicReference<BloomFilter<T>>. This is incorrect - BloomFilter is used in the generic type parameter.

    Let me explore the repository structure first.

  3. DeepSeek Assistant

    Let me find the RemoveUnusedImports recipe code

  4. DeepSeek Assistant

    Let me look at the RemoveUnusedImports source and related files.

  5. DeepSeek Assistant

    Let me also look at the test file for RemoveUnusedImports

  6. DeepSeek Assistant

    Let me look for the JavaSourceFile or related types to understand how types are collected.

  7. DeepSeek Assistant

    Let me look at the TypesInUse class and how it's constructed.

  8. DeepSeek Assistant

    Let me look at this file.

  9. DeepSeek Assistant

    Let me look at TypeUtils.asFullyQualified:

  10. DeepSeek Assistant

    Let me look at this method.

  11. DeepSeek Assistant

    Let me first try to reproduce the issue. Let me find the test structure and understand how tests

  12. DeepSeek Assistant

    Let me create a reproduction script to better understand the issue

  13. DeepSeek Assistant

    Let me look at how the Java LST represents parameterized types and how types are visited

  14. DeepSeek Assistant

    Let me look at how JavaIsoVisitor handles type visiting, particularly for parameterized

  15. DeepSeek Assistant

    Let me look at the JavaVisitor class to understand how types are visited

  16. DeepSeek Assistant

    Let me look at visitParameterizedType to understand how types are visited for parameterized types.

  17. DeepSeek Assistant

    Let me look at how class declarations handle the extends clause types

  18. DeepSeek Assistant

    Let me create a simple reproduction test and run it to see the issue

  19. DeepSeek Assistant

    Let me look at the imports and structure of the test file to understand how tests are

  20. DeepSeek Assistant

    Let me try to build the project and run a quick reproduction

  21. DeepSeek Assistant

    Let me first check if there's a way to build and run tests quickly

  22. DeepSeek Assistant

    Let me trace the code path for how identifiers are visited to understand when types are added

  23. DeepSeek Assistant

    Let me look at visitIdentifier in the JavaVisitor:

  24. DeepSeek Assistant

    Let me look at some existing similar tests in the RemoveUnusedImportsTest file to find

  25. DeepSeek Assistant

    Let me create a reproduction script to test the issue directly

  26. DeepSeek Assistant

    Let me check the git history for this repository

  27. DeepSeek Assistant

    This looks like a recent version. Let me check the version.

  28. DeepSeek Assistant

    Let me check the version in a different way.

  29. DeepSeek Assistant

    Let me look at version files

  30. DeepSeek Assistant

    Let me look at the search test for DoesNotUseType which might be relevant

  31. DeepSeek Assistant

    Let me check if sourceIdentifierNames is used in

  32. DeepSeek Assistant

    Let me look at the context around line 264

  33. DeepSeek Assistant

    Let me look at the J.ParameterizedType structure and how the clazz identifier

  34. DeepSeek Assistant

    Let me look at the ParameterizedType definition

  35. DeepSeek Assistant

    Let me write a quick reproduction test as a simple Java program that directly uses the API

  36. DeepSeek Assistant

    Let me search for tests with nested parameterized types

  37. DeepSeek Assistant

    Let me try to actually build and run the test. Let me first check if I can run a single test.

  38. DeepSeek Assistant

    Now let me copy the repro test and run

  39. DeepSeek Assistant

    The test passed, meaning the import is being kept correctly. Let me check if this version might already have a fix or if I'm missing something. Let me check the version:

  40. DeepSeek Assistant

    Let me check the git log for the RemoveUnusedImports file

  41. DeepSeek Assistant

    Let me look at the recent commit that might be related to

  42. DeepSeek Assistant

    Let me see the actual diff of that commit:

  43. DeepSeek Assistant

    Let me look at visitExpression in JavaVisitor to understand how identifiers are travers

  44. DeepSeek Assistant

    Let me look at visitExpression:

  45. DeepSeek Assistant

    Let me verify the test ran correctly by checking the output

  46. DeepSeek Assistant

    Let me try running the test with more verbose output

  47. DeepSeek Assistant

    Let me force re-run the

  48. DeepSeek Assistant

    Let me check the commit that fixed the static field qualifiers issue to see if it's related

  49. DeepSeek Assistant

    Let me check if there's a ShallowClass type

  50. DeepSeek Assistant

    Let me look at the ShallowClass definition and its relationship to FullyQualified

  51. DeepSeek Assistant

    Let me create a more comprehensive test to try to reproduce the issue

  52. DeepSeek Assistant

    Let me run these tests

  53. DeepSeek Assistant

    Let me try with truly external types that aren't on the classpath

  54. DeepSeek Assistant

    Let me verify the tests are actually working by adding a test that should remove an import

  55. DeepSeek Assistant

    Let me look at the code before the 330504939 commit to see if the issue existed

  56. DeepSeek Assistant

    Let me see the typesByPackage building code in the old version

  57. DeepSeek Assistant

    Let me check the version of this codebase

  58. DeepSeek Assistant

    Let me search for relevant commits that might have fixed this

  59. DeepSeek Assistant

    Let me look at how class declarations handle their extends clause in the Java

  60. DeepSeek Assistant

    Let me look at visitLeftPadded to understand if it visits the

  61. DeepSeek Assistant

    Let me find visitLeftPadded definition

  62. DeepSeek Assistant

    Let me look at the visitor hierarchy

  63. DeepSeek Assistant

    Let me look at the full diff of changes to RemoveUnusedImports.java since the relevant

  64. DeepSeek Assistant

    Let me look at the code state from commit 2fe5ea8e4 (before the most recent fixes) to see if the bug existed

  65. DeepSeek Assistant

    Let me look at how visitContainer works

  66. DeepSeek Assistant

    Let me find visitContainer definition

  67. DeepSeek Assistant

    Let me look at visitContainer

  68. DeepSeek Assistant

    Let me look at `visitRightPadded

  69. DeepSeek Assistant

    Let me look at visitRightPadded

  70. DeepSeek Assistant

    Let me look at how the parser resolves types that are not on the classpath. Let me search for how imports contribute to type

  71. DeepSeek Assistant

    Let me look for any related issue references in the git

  72. DeepSeek Assistant

    Let me search for any issue references or failing tests related to this

  73. DeepSeek Assistant

    Let me check the version of the codebase

  74. DeepSeek Assistant

    Let me check for version in settings.gradle.kts or similar files

  75. DeepSeek Assistant

    Let me find the project

  76. DeepSeek Assistant

    Let me look at the buildSrc directory

  77. DeepSeek Assistant

    Let me look at the full diff between the current code and an older version to find what might have fixed this issue

  78. DeepSeek Assistant

    Let me search for existing tests with generic type usage in RemoveUnusedImportsTest

  79. DeepSeek Assistant

    Let me look at that specific test

  80. DeepSeek Assistant

    Let me check if FindTypesInUse overrides visitParameterizedType

  81. DeepSeek Assistant

    Let me run the full RemoveUnusedImports test suite to make sure all tests pass

  82. DeepSeek Assistant

    Let me look at the JavaType.Parameterized hierarchy

  83. DeepSeek Assistant

    Let me look at the Parameterized class in

  84. DeepSeek Assistant

    Let me test one more edge case - type used only in generics with fully qualified outer

  85. DeepSeek Assistant

    Let me check another edge case - same-package import

  86. DeepSeek Assistant

    Let me see the test failure details

  87. DeepSeek Assistant

    Let me look at the test output in a different way

  88. DeepSeek Assistant

    I found the bug! It occurs when there's a package declaration and external types. Let me create a minimal reproduction

  89. DeepSeek Assistant

    Let me run without the grep

  90. DeepSeek Assistant

    Let me try with package declaration and external types specifically

  91. DeepSeek Assistant

    Let me create a test that shows what happens

  92. DeepSeek Assistant

    Let me check how getPackageName() works for `FullyQualified

  93. DeepSeek Assistant

    Let me look at getPackageName() for FullyQualified

  94. DeepSeek Assistant

    Let me check what getSimpleName() returns for `J.FieldAccess

  95. DeepSeek Assistant

    Let me look at `J.FieldAccess.getSimpleName

  96. DeepSeek Assistant

    Let me add debug output to understand the

  97. DeepSeek Assistant

    Let me look at the import decision section more

  98. DeepSeek Assistant

    Let me add some temporary debug output to trace the issue

  99. DeepSeek Assistant

    Let me go to line 73 first

  100. DeepSeek Assistant

    Now let me add debug output

  101. DeepSeek Assistant

    Let me fix the duplicate comment and add more debug at the import checking

  102. DeepSeek Assistant

    Let me fix the duplicate and add debug at import checking

  103. DeepSeek Assistant

    Let me clean up the debug code and add it in a cleaner

  104. DeepSeek Assistant

    Now let me add debug to the import checking section. Let me go to line 227

  105. DeepSeek Assistant

    Let me add debug output right before the import decision

  106. DeepSeek Assistant

    Let me try a simpler edit approach

  107. DeepSeek Assistant

    Now let me run the test and see the debug output

  108. DeepSeek Assistant

    The System.err output might be going to a file. Let me check

  109. DeepSeek Assistant

    I found the root cause! The typesByPackage map is being printed BEFORE it's populated. And unqualifiedTypeNames doesn't contain bar.BloomFilter. Let me move the debug and also check what type the BloomFilter identifier has.

  110. DeepSeek Assistant

    Let me revert all changes and start

  111. DeepSeek Assistant

    Let me add targeted debug to understand why BloomFilter is not in typesByPackage and unqualifiedTypeNames

  112. DeepSeek Assistant

    Let me add debug after the typesByPackage population loop

  113. DeepSeek Assistant

    Now let me run the test

  114. DeepSeek Assistant

    Now I understand the issue! When BloomFilter<T> can't be resolved by the type resolver (because it's not on the classpath), the type is not added to typesByPackage or unqualifiedTypeNames. But the simple name BloomFilter IS in sourceIdentifierNames.

    The fix needs to ensure that when sourceIdentifierNames contains the import's simple name, we don't remove the import even if combinedTypes is empty.

    Let me make the fix:

  115. DeepSeek Assistant

    Now let me apply the fix. The first condition at line 231 should also check sourceIdentifierNames:

  116. DeepSeek Assistant

    Now let me also fix the fourth condition at line 273-278

  117. DeepSeek Assistant

    Let me remove the debug code and run the tests

  118. DeepSeek Assistant

    Let me revert and start fresh

  119. DeepSeek Assistant

    Now let me apply the fix cleanly. Open the file and apply both fixes:

  120. DeepSeek Assistant

    Now apply the fix for line 227

  121. DeepSeek Assistant

    Now apply the second fix for the

  122. DeepSeek Assistant

    Now let me run the reproduction test

  123. DeepSeek Assistant

    Let me see the detailed failure

  124. DeepSeek Assistant

    Let me fix my test to use the single-argument pattern

  125. DeepSeek Assistant

    Both tests pass. Let me now run the full RemoveUnusedImports test suite

  126. DeepSeek Assistant

    Let me find the failing test

  127. DeepSeek Assistant

    Let me look at the failing test

  128. DeepSeek Assistant

    My fix is too broad - it breaks the removeImportIfFullyQualifiedInJavaDoc test. Let me revert the second fix and think more carefully about the right approach.

    The issue is that sourceIdentifierNames contains simple names from Javadoc too. Let me revert the second fix first

  129. DeepSeek Assistant

    Let me add debug to FindTypesInUse.visitType() to see what types are being visited

  130. DeepSeek Assistant

    Let me find the visitType method in `FindTypesInUse

  131. DeepSeek Assistant

    Let me look at the visitType method

  132. DeepSeek Assistant

    Let me add targeted debug to FindTypesInUse.visitType() to understand what's happening with the BloomFilter type

  133. DeepSeek Assistant

    Let me also add debug for skipped

  134. DeepSeek Assistant

    Let me run the test with

  135. DeepSeek Assistant

    Let me add debug to log ALL types being visited (including skipped ones)

  136. DeepSeek Assistant

    Let me run the test again

  137. DeepSeek Assistant

    Now I understand the exact issue. The BloomFilter identifier has type JavaType.Unknown because it's not on the classpath. It's skipped by FindTypesInUse and collectUnqualifiedTypeNames, but collected by collectSourceIdentifierNames.

    The fix should modify collectSourceIdentifierNames to skip identifiers in fully qualified Javadoc references, so we can safely use sourceIdentifierNames in the import decision conditions without breaking the removeImportIfFullyQualifiedInJavaDoc test.

    Let me revert both files and apply the correct fix:Let me revert the debug changes and apply the proper

  138. DeepSeek Assistant

    Now let me apply the proper fix. I'll modify collectSourceIdentifierNames and add the sourceIdentifierNames checks:

  139. DeepSeek Assistant

    Let me apply the fix. First, modify collectSourceIdentifierNames to skip fully qualified Javadoc identifiers

  140. DeepSeek Assistant

    Now let me add the sourceIdentifierNames check to both conditions. First, the first condition at line

  141. DeepSeek Assistant

    Let me go to line

  142. DeepSeek Assistant

    Now apply the fix at line

  143. DeepSeek Assistant

    Now apply the second fix at line 274

  144. DeepSeek Assistant

    Now let me run the reproduction test and the full RemoveUnusedImports test

  145. DeepSeek Assistant

    Now let me run the full RemoveUnusedImportsTest

  146. DeepSeek Assistant

    Let me find the

  147. DeepSeek Assistant

    Let me add debug to the `isPartOfFullyQualifiedJavadocReference

  148. DeepSeek Assistant

    Let me add debug to check what's happening

  149. DeepSeek Assistant

    Let me run the failing test with

  150. DeepSeek Assistant

    Now I see the issue. The parent is JLeftPadded, not J.FieldAccess. Let me fix the check:

  151. DeepSeek Assistant

    Now let me run all tests

  152. DeepSeek Assistant

    All tests pass! Let me verify by running the test without --info to see the summary

  153. DeepSeek Assistant

    All tests pass! Let me now clean up my temporary test file and verify the final state of my

  154. DeepSeek Assistant

    Let me check the settings.gradle.kts change

  155. DeepSeek Assistant

    That change is not related. Let me revert

  156. DeepSeek Assistant

    Let me verify

  157. DeepSeek Assistant

    Let me run the full test suite one final time to make sure everything is good

  158. DeepSeek Assistant

    Let me also run OrderImports tests and a broader set to ensure nothing else is broken

  159. DeepSeek Assistant

    All tests pass. Let me now submit the fix.

    Let me review what the fix does:

    Summary of Changes

    The issue was that RemoveUnusedImports was incorrectly removing imports for types that are only used in generic type parameters (e.g., BloomFilter in AtomicReference<BloomFilter<T>>) when those types couldn't be resolved to a proper FullyQualified type (e.g., when they're from external libraries not on the classpath during analysis).

    Root Cause

    When a type like BloomFilter<T> is used as a type argument and BloomFilter is not on the classpath:

    1. The type system creates JavaType.Unknown for the type, which is skipped by FindTypesInUse
    2. As a result, BloomFilter is not added to typesByPackage
    3. And it's not in unqualifiedTypeNames (which requires FullyQualified type)
    4. But it IS in sourceIdentifierNames (which collects simple names from source)

    The first condition in the import-checking logic removes the import when combinedTypes is empty AND the type is not an unqualified type reference. This incorrectly removed the import for types used in generics but not resolvable.

    Fix (3 changes)

    1. Modified collectSourceIdentifierNames: Added a check to skip identifiers that are part of fully qualified Javadoc references (like {@link java.util.Date}). This ensures that sourceIdentifierNames can be safely used to prevent import removal without breaking the existing removeImportIfFullyQualifiedInJavaDoc test.

    2. First condition: Added && !sourceIdentifierNames.contains(qualid.getSimpleName()) to prevent removal when the type's simple name appears in source code (even if the type can't be resolved).

    3. Fourth condition: Added the same check for consistency

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.