Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intellij IDEA doesn't grey out some unused methods

In intellij IDEA, if a method is unused, the method is shown in a gray color. But in some cases, IDEA doesn't grey out the method, but when I check the references of those methods using alt + F7, IDEA says that the method is unused.

Is this a IDEA bug or is there any reason why IDEA wouldn't grey out these specific methods? If it is a bug, is there some workaround to make IDEA identify that method is unused?

like image 743
Lahiru Chandima Avatar asked Aug 17 '15 10:08

Lahiru Chandima


People also ask

How do I delete unused variables in IntelliJ?

In IntelliJ, in a . java file, some unused code is greyed out indicating that the declared variable or function is never used. Unused imports are removed using Ctrl+Alt+O.


2 Answers

I used to have it working like charm, but one time, I by mistake clicked on alt+Enter on an unused method, and chose to suppress the inspection on unused code. Ever since then, I stopped getting the grayed out methods and code, so since there is a way to get it undone, there sure must be a way to get it back working.

After 5 minutes of searching, I found a solution:

Settings --> Editor --> Inspections --> Java --> Declaration Redundancy --> Unused Declaration

Make sure you check "Unused Declaration"

And I just checked by creating a new useless method, working like a charm.

like image 154
Amine Messabhia Avatar answered Sep 20 '22 15:09

Amine Messabhia


Most likely it's not a bug, it's a limitation for performance reasons. Methods likely to take a long time when searching for usages are skipped.

A workaround is to run Unused Declaration inspection explicitly in all your project via Analyze | Inspect Code or Analyze | Run Inspection by Name. That'll take some time. You can also set up TeamCity server to do it for you automatically every night.

like image 35
Peter Gromov Avatar answered Sep 18 '22 15:09

Peter Gromov