Disable "not used" warning for public methods of a class

Intellij IdeaPublic Method

Intellij Idea Problem Overview


The new intellij upgrade (10.5) now shows a warning that some of the methods defined for a class are not being used. These methods are public and I plan on not using all of them as I have created them to support the API expected. I would like to disable this warning (not used for public methods in a class). Is there a way to do it?.

Intellij Idea Solutions


Solution 1 - Intellij Idea

You can disable it for a single method like this

@SuppressWarnings("unused")
public void myMethod(){...}

Solution 2 - Intellij Idea

IDEA 2016.3

In the upcoming version IDEA 2016.3 (preview version already available) it is now possible to adjust the inspection scope:

enter image description here

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

IDEA 14.x

In IntelliJ IDEA 14.0.x the settings are under:

Settings | Editor | Inspections | Declaration redundancy | Unused symbol/declaration

In IntelliJ IDEA 14.1 the option appears to be gone..

Solution 3 - Intellij Idea

Disable Settings | Inspections | Declaration redundancy | Unused Declaration code inspection. As an option you can create a custom scope for your API classes and disable this inspection only per API scope so that it still works in the rest parts of your project.

Solution 4 - Intellij Idea

2018-2019

Here is the 2019 update for:
IntelliJ IDEA 2018.3.2 (Community Edition)
Build #IC-183.4886.37, built on December 17, 2018


Settings | Editor | Inspections | Declaration redundancy | Unused declaration

enter image description here

Solution 5 - Intellij Idea

I think the best way to avoid the highlighting of that unused public methods is writing a couple of test for those methods in your API.

Solution 6 - Intellij Idea

In the latest version, this options is under Settings>Inspections>Java>Declaration redundancy>Unused declaration>Methods uncheck options which are not required.

Solution 7 - Intellij Idea

This is an old thread, but I ended up here faster than I could find a solution so I'm going to go ahead and share my findings. First, I am not sure if we are working with the same language (JS here,) but fiddling with the GUI-based tools, here is what I ended up with. The following code was giving me the infamous "not used" warning:

/**
 * @class sample class
 */
var MyClass = function () {
    return this;
};

/**
 * Some public method
 * @api public
 */
MyClass.prototype.myMethod = function () {
    return null;
};

There goes the "Unused definition myMethod" The inspector ended up suggesting to ignore this specific issue by adding

//noinspection JSUnusedGlobalSymbols

right on top of this specific method so that the following code no longer results in this warning:

//noinspection JSUnusedGlobalSymbols
/**
 * Some public method
 * @api public
 */
MyClass.prototype.myMethod = function () {
   return null;
};

Other warnings (typoes etc..) still seem to show up, including unused local variables and parameters, so it seems to isolate this particular issue. The downside is that it tends to pollute your code if you have lots of it...

Solution 8 - Intellij Idea

I just clicked "suppress for statement" and webstorm prepended this:

//noinspection JSUnusedGlobalSymbols

Solution 9 - Intellij Idea

When extending a library recently, I was also alerted by that "not used" inspection warning.

Think about why IntelliJ signals

Usually when doing refactoring all unused methods/parameters should be safe to be deleted (via Intellij's safe delete action).

This way the intend of IntelliJ (like Checkstyle and others) is to support our clean design. Since the unused methods are neither used internally (in src/java/main) nor externally tested (in src/java/test) they seem obsolete. So why not following the saying "When in doubt, throw it out".

When refactoring, that's mostly a true advice. But if we are developing a library/API that is intended to be used by other codebases (modules/dependencies from the ouside), then we rather answer "When not used, get confused".

We are astonished about IntelliJ's warning. The methods should not be deleted, because they are actually intended to be used elsewhere. They are entry-points.

Then choose suitable solution

All of below solutions have one in commen:

  • Communicate through your code, so every IDE and developer can understood (e.g. add a test so it becomes used)
  • Tell your intend (e.g. to IntelliJ via reconfiguring Code Inspection)
Configure Inspection or Disable

As described in various earlier answers. With screenshots and navigation hints to IntelliJ's Code Inspection settings

Add a test

If you add a test for the unused (method, class etc.) you will benefit in 3 ways:

  1. correctness: (previously) unused subject under test (SUT) is tested
  2. communication: you clearly communicate to every reader, that and how your unused (method, class, etc.) should be used
  3. clearance: now the unused finally got used. So IntelliJ's inspection would not find and warn anymore.
Add or mark as Entry Point

I saw the suggestion multiple times:

> Add the element as an entry point. By default, all code in the global scope as well as tests is treated as reachable. If you know that a method or function is executed, you may add it as an entry point. The code inside the entry point is now executed and reachable, as well. When you add an entry point, your source code files stay unaffected, and the element’s record is stored with the project under .idea\misc.xml.

Solution 10 - Intellij Idea

Maybe the entry points funtion can work, where you can specify the code pattern that can disable the warning
Settings | Inspections | Declaration redundancy | Unused Declaration | entry point

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVijaykumar KrishnaswamyView Question on Stackoverflow
Solution 1 - Intellij IdeaJT MontanaView Answer on Stackoverflow
Solution 2 - Intellij IdeaDarek KayView Answer on Stackoverflow
Solution 3 - Intellij IdeaCrazyCoderView Answer on Stackoverflow
Solution 4 - Intellij IdeaBenView Answer on Stackoverflow
Solution 5 - Intellij IdeacsansbView Answer on Stackoverflow
Solution 6 - Intellij IdeaVijay NandwanaView Answer on Stackoverflow
Solution 7 - Intellij IdeaQuentinView Answer on Stackoverflow
Solution 8 - Intellij IdeaGuntramView Answer on Stackoverflow
Solution 9 - Intellij Ideahc_devView Answer on Stackoverflow
Solution 10 - Intellij IdeajackyView Answer on Stackoverflow