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?.
You can disable it for a single method like this
@SuppressWarnings("unused")
public void myMethod(){...}
@Suppress("unused")
. Also, it can found in right click -> disable inspection for this line. –
Hesperidin IDEA 2016.3
In the upcoming version IDEA 2016.3 (preview version already available) it is now possible to adjust the inspection scope:
< 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".
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..
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 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.
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.
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
In the latest version, this options is under Settings>Inspections>Java>Declaration redundancy>Unused declaration>Methods
uncheck options which are not required.
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...
I just clicked "suppress for statement" and webstorm prepended this:
//noinspection JSUnusedGlobalSymbols
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:
- correctness: (previously) unused subject under test (SUT) is tested
- communication: you clearly communicate to every reader, that and how your unused (method, class, etc.) should be used
- 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:
- as optional dialog tab inside IntelliJ's Inspection Settings
- as comment below top-ranked answer:
IMO better approach is to mark class as "entry point". – Tanya Jivvca Aug 18 at 8:46
- in IntelliJ's forum: Code inspection: unused element - what is an entry point? (emphasis in below quote by me):
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
.
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
© 2022 - 2024 — McMap. All rights reserved.