ChatGPT解决这个技术问题 Extra ChatGPT

How to use IntelliJ IDEA to find all unused code?

When I am in a .java file the unused 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 codes. How can I do that in IntelliJ IDEA?

The answer given is right, but it's just a pity that there is no way to get all unused /dead code inside a project + a button to remove it all. That would be helpful !
@Snicolas right-click on the inspection result (Declaration redundancy->Unused declaration) and choose "Safe delete".
Or if you want button, there's the light-bulb in the left toolbar.
You may check my answer on stackoverflow.com/questions/22522013/…. There you can find how to filter the results to see only those, which you really wanted.
Is there a way of automating IntelliJ's unused code detection mechanism in a pull request ? When a pull request is created, so it will scan through the files in that PR, and show me the unused code.

B
Borek Bernard

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"


Just a correction for future readers: IDEA 12.1.6, at least, does indeed have it under Analyze --> Inspect Code
@CrazyCoder This shows unused fields and methods too, any way to show only unused classes?
My 2 cents: on IntelliJ 2016.3.4 the option is under Analyze > Run Inspection By Name. -- My 5 cents: if you're working on a big project and you're running the feature for the whole project, I suggest you to do it before lunch or at the end of the day, because it can take 1h+ to complete. Or, maybe, you can do it during the work hours and say to your boss that you're running an analyzer... and you can get some rest for free! :)
@BenClayton and the rest, looking just for unused classes....stackoverflow.com/a/38244028/2541200
If you are trying to find unused methods in a Kotlin project, run the Unused symbol inspection. Looks like the Unused declaration only works in Java projects.
J
Johnny

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:

https://i.stack.imgur.com/ngUH5m.png

Than, pick Unused declaration:

https://i.stack.imgur.com/DGDd6m.png

And finally, uncheck the Include test sources:

https://i.stack.imgur.com/1Ls86m.png


In newer versions of IntelliJ it's under Code | Analyze Code | Run Inspection by Name
O
Orwellophile

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.

https://i.stack.imgur.com/QmUrH.png