Why does "Refactor" suddenly remove unused imports?

已回答

I have used “Refactor>Rename” (Shift-F6) for a long time.   There seems to be a new “Feature” - Renaming a static string seems to remove unused imports from my Java class.

Refactoring “private static String BLAHBLAH” to “private static String YOYOYO” seems to walk through the whole file and make not only that change but other changes it wants to make, such as removing an unused import (and God knows what else.)

I have checked the “Optimize Imports” setting (Settings>Tools>Actions on save) and it is NOT CHECKED.

I also reset my Git branch and tried this again to make sure I wasn't crazy.    Simply renaming one variable removes this import.  

Why?   I work in a very controlled environment and I have to account for EVERY SINGLE LINE I CHANGE.   Why is Intellij suddenly doing this?

NOTES:   Intellij IDEA 2023.1.2 (Ultimate Edition) Build IU-231.9011.34

1

If the issue still occurs in the current IntelliJ IDEA release version, please share a sample project and the exact steps to reproduce it.

Note that you may have settings to reformat code / optimize imports enabled in the Version Control | Before Commit or in Settings | Tools | Actions on Save.

0

In version 2024.1.1 (Build #IC-241.15989.150, built on April 29, 2024) it is the same with the not used imports. By renaming the file it will be removed. I have tried to deactivate all import optimatize function under:

File -> Settings -> Tools -> Actions on Save → Optimize imports

File -> Settings -> Editor -> General -> Auto Import -> Optimize imports on the fly

File -> Settings -> Version Control -> Commit → Optimize imports

But the not used imports were still removed. Give it more otions to change?

0

Hello all, I've discussed this with our Java team developer, and this behavior is by design and thus expected. This also behaviors similar to other languages; see:

https://youtrack.jetbrains.com/issue/PY-54707/Move-refactoring-shouldnt-remove-unused-imports.

If the import is mistakenly removed because it was actually used, please provide a sample code for that, and we will create an issue and then fix it.

0

请先登录再写评论。