开发者

How to use IntelliJ IDEA to find all unused code?

When I am in a .java file, the unu开发者_运维问答sed code is usually grayed out or has a green underline saying this code will probably (probably because of some weird JNI/Reflection corner cases) be unused. But I have this project with thousands of Java files and I want to find all instances of such probable-unused code. How can I do that in IntelliJ IDEA?


Just use Analyze | Inspect Code with appropriate inspection enabled (Unused declaration under Declaration redundancy group).

Using IntelliJ 11 CE you can now "Analyze | Run Inspection by Name ... | Unused declaration"


In newer versions of IntelliJ it's under Code -> Analyze Code -> Run Inspection by Name.

In IntelliJ versions before that, you should run it from Analyze -> Run Inspection By Name:

How to use IntelliJ IDEA to find all unused code?

Than, pick Unused declaration:

How to use IntelliJ IDEA to find all unused code?

And finally, uncheck the Include test sources:

How to use IntelliJ IDEA to find all unused code?


After you've run the Inspect by Name, select all the locations, and make use of the Apply quick fixes to all the problems drop-down, and use either (or both) of Delete unused parameter(s) and Safe Delete.

Don't forget to hit Do Refactor afterwards.

Then you'll need to run another analysis, as the refactored code will no doubt reveal more unused declarations.

How to use IntelliJ IDEA to find all unused code?


A picture is worth a thousand words! IntelliJ IDEA 2022.x.x

  • Go to the code tab, analyze code, and run inspection by name.

How to use IntelliJ IDEA to find all unused code?

  • Select what you need
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜