Turn off unused code warning for public functions in IntelliJ

3.4k Views Asked by At

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?

3

There are 3 best solutions below

2
On

Turn off this inspection: File -> Settings -> Inspections(under Project Settings) -> Unused declaration(under Declaration redundancy group), move the hook before it.

enter image description here

5
On

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.

0
On

... 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.