I try to ignore or disable some useless warnings in eclipse by checkstyle with annotation @SuppressWarnings
like
How to disable a particular checkstyle rule for a particular line of code?
but this don't work for me.
Here is the checkstyle.xml
<module name="Checker">
<property name="severity" value="warning"/>
<module name="SuppressWarningsFilter"/>
<module name="TreeWalker">
<property name="tabWidth" value="4"/>
<module name="FileContentsHolder"/>
<module name="SuppressWarningsHolder"/>
<module name="CyclomaticComplexity"/>
...
and here the java code:
@SuppressWarnings("checkstyle:CyclomaticComplexity")
public void doSomeThing() {
...
}
Also set the value of warning to "all" @SuppressWarnings("all")
or @SuppressWarnings("CyclomaticComplexity")
, @SuppressWarnings("cyclomaticcomplexity")
is without any result.
The documentation of checkstyle is poor. Some ideas?
A. SuppressWarnings Filter
With checkstyle 6.5.0 I can use @SuppressWarnings. Please consider following points:
Some examples for the checkstyle module "MagicNumber":
Works:
.
Does not work:
.
.
Further notes
I got a warning unsupported suppresswarnings which I disabled in the Eclipse preferences with Java=>Compiler=>Errors/Warnings=>Annotations=>Unhandled token in '@SuppressWarnings'': Ignore
The name (as defined in the xml file) of the corresponding checkstyle module is not shown in the violation message that pops up when hovering over a code issue. I enabled the option "include module id (if available) in violation message" and manually altered all module ids to be the same as the corresponding module name in the xml file, but lower case. For example there is a module <name="AnonInnerLength"> which is displayed in the Eclipse checkstyle settings as "Anonymous inner classes length". That module had no module id. I changed the module id to checkstyle:anoninnerlength to make it easier for my colleagues to suppress the warning:
<module name="AnonInnerLength">
<property name="id" value="checkstyle:anoninnerlength"/>
<module>
I use the prefix "checkstyle:" in the module id as well as in the SuppressWarnings tag to make it explicit that the warning is not a "standard Eclipse warning". (The optional prefix "checkstyle:" could already be used in the tag without altering the module id. However, the prefix would not be shown in the violation message. Including it in the module id makes the message more transparent and motivates my colleagues to include the prefix in the tag, too.)
B. Suppression Comment Filter
Works:
.
Does not work:
C. Example checkstyle settings.xml file:
My checkstyle version is
8.1
.It works with gradle config like this:
build.gradle
:And ignore magic number like this:
NOTE: prefix
checkstyle:
is optional. Hope this can help someone.