Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn off unused code warning for public functions in IntelliJ

Eclipse was smart about this; IntelliJ not yet. Of course if a function is unused (and untested, I guess I should feel bad for that - even if it's just returning a simple variable, or implementing someone else's interface) but public, it may be used elsewhere. Looking under Inspections and searching for "unused," I don't see visibility settings. Does this granularity exist?

like image 939
djechlin Avatar asked May 28 '14 23:05

djechlin


2 Answers

If you want to highlight unused public methods, please enable the "Settings|Inspections|Declaration redundancy|Unused declaration" global inspection.

If you want to highlight unused private methods, please enable the "Settings|Inspections|Declaration redundancy|Unused symbol" local inspection.

So, if you want to highlight unused private members, but do not highlight unused public members, turn off "Unused declaration" and turn on "Unused symbol".

Source

I've just tested it using IDEA 13.1.4, and it worked exactly as described.

like image 113
Darek Kay Avatar answered Oct 25 '22 16:10

Darek Kay


... for Kotlin

as of IntelliJ IDEA 2017.3.4 (and probably earlier versions), the corresponding setting is:

File | Settings | Editor | Code Style | Inspections | Kotlin | Redundant constructs | Unused symbol

documentation:

This inspection reports classes, functions or properties in the specified inspection scope that are not used or not reachable from entry points.

There is no setting for Unused declaration anymore, so this setting is not for private and public symbols alike.

Alternative

Instead of completely turning off warnings about unused symbols, you could use an annotation (possibly defined by yourself), for example @PublicApi, to mark all function sand classes that you do not want to get warnings for. You then have to add this annotation as an entry point under:

File | Settings | Editor | Code Style | Inspections | Kotlin | Redundant constructs | Unused symbol | Options | Annotations...

You may have to restart the IDE after that.

like image 30
hoijui Avatar answered Oct 25 '22 15:10

hoijui