Intellij IDEA doesn't grey out some unused methods

后端 未结 3 697
别跟我提以往
别跟我提以往 2021-01-17 17:08

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

相关标签:
3条回答
  • 2021-01-17 17:19

    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.

    0 讨论(0)
  • 2021-01-17 17:24

    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.

    0 讨论(0)
  • 2021-01-17 17:31

    It might be a bug if you're using a method with a very common name.

    If you tried @Peter Gromov method above, and your method is still yellow, it might be the case that this is a bug.

    I had a very common named method, named "stop".

    • Looking for usages (using ALT + F7) didn't show anything.
    • Analyzing the whole project, clearly showed that this method did not have any use.

    Despite that, the method was still yellow.

    I was surprised to find out, that if I try to refactor the method name, I get a pop-up warning that this will change in other places as well.

    Turns out, that the refactoring warned about changing the method name in TODO comments. Somehow Lint recognized the TODO comments as using this method.

    My advice is to just not name your methods as something that may be written in a TODO comment.

    See this image, where I am using a method named "stop" :

    0 讨论(0)
提交回复
热议问题