Disable "not used" warning for public methods of a class
Asked Answered
B

10

96

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

Brest answered 29/5, 2011 at 6:50 Comment(0)
D
78

You can disable it for a single method like this

@SuppressWarnings("unused")
public void myMethod(){...}
Diphenylhydantoin answered 10/1, 2015 at 18:48 Comment(6)
Perfect! I didn't want the warning off for my new code, just wanted it to shut up about legacy code that might "reflect in" through some piece of "Spring-etti".Jugglery
It is a bit silly that you need to tell Intellij that a PUBLIC method can be used by code Intellij doesn't see...Trod
It's sad really. I find Intellij very annoying tbh. I guess their intention were good, but I find myself fighting it while coding. The right hand side scroll bar is full of false flag warnings instead of real problems warnings.Mildredmildrid
@TatiOverflow: By right-clicking the right bar and selecting to "Customize Highlighting Level" you can remove inspections altogether.Bodgie
It seems to be renamed to @Suppress("unused"). Also, it can found in right click -> disable inspection for this line.Hesperidin
This isn't intellij's fault. This Java's fault for having bad visibility scopes.Mer
G
65

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

Gorlin answered 29/9, 2014 at 8:40 Comment(6)
It seems like the 'Unused symbol' inspection has gone in IDEA 14.Paquin
Not at all, it's under Settings | Editor | Inspections | Declaration redundancy | Unused symbol. If you can't find it, type Unused symbol in the search box in the settings dialog.Gorlin
I tried that already. I'm at IDEA EAP 141.814.3. Here's the screenshot - imgur.com/cfOa48MPaquin
You're right, in 14.0.3 it was still there. Now I can't find it or any alternative.Gorlin
There's a problem report filed and we should vote for it so JetBrains fixes this - youtrack.jetbrains.com/issue/IDEA-148321Paquin
An improved option is available in the latest EAP version (updated answer)Gorlin
B
42

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.

Beiderbecke answered 30/5, 2011 at 8:12 Comment(3)
It's too bad there's no option to only disable this inspection for public methods and keep it enabled for private methods.Tootsie
There is, see my other answer.Gorlin
It's gone again. See comments under @DarekKay answer.Albeit
W
12

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.

Wilona answered 24/11, 2016 at 13:23 Comment(0)
S
12

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

Shun answered 15/1, 2019 at 20:0 Comment(5)
Is it necessary to have the pro version to get all those options ? Because I do not have them, I only have "Unused declaration". Maybe it is because I am working on a groovy project ... but still.Trellis
This is because I was working on a groovy project, they are there in javaTrellis
IMO better approach is to mark class as "entry point".Depilate
Picture worth more than 1000 words. Can you add an explanation and link to IntelliJ help-site in order to enable users understand this feature & configuration? For example watch Trisha Gee explaining Unused Code Detection in IntelliJDevereux
@TanyaJivvca Your suggestion inspired me research and answer: we should communicate the intended use with a test, so it becomes used and documented (as entry point) 😉Devereux
W
8

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

Walsh answered 14/4, 2017 at 5:35 Comment(0)
S
2

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

Skyline answered 19/6, 2014 at 6:39 Comment(2)
Exactly. This is code pollution when a dev at Intellij could simply fix the issue. Anyways, any new idea on this?Mildredmildrid
Note: You can let phpstorm add "//noinspection JSUnusedGlobalSymbols" automatically by using the "Show intention actions" dialog > "Suppress for statement". I can access this with a keyboard shortcut alt+enter depends on your key mapping.Indorse
I
2

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

//noinspection JSUnusedGlobalSymbols
Irreverence answered 24/3, 2017 at 11:33 Comment(0)
D
2

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 intent (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:

IMO better approach is to mark class as "entry point". – Tanya Jivvca Aug 18 at 8:46

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.

Devereux answered 12/12, 2020 at 10:56 Comment(0)
C
0

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

Carlcarla answered 12/7, 2021 at 8:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.