Since Checkstyle 5.0
Allows to specify what warnings that @SuppressWarnings
is not allowed to suppress.
You can also specify a list of TokenTypes that
the configured warning(s) cannot be suppressed on.
Limitations: This check does not consider conditionals inside the @SuppressWarnings annotation.
For example:
@SuppressWarnings((false) ? (true) ? "unchecked" : "foo" : "unused")
.
According to the above example, the "unused" warning is being suppressed
not the "unchecked" or "foo" warnings. All of these warnings will be
considered and matched against regardless of what the conditional
evaluates to.
The check also does not support code like @SuppressWarnings("un" + "used")
,
@SuppressWarnings((String) "unused")
or
@SuppressWarnings({('u' + (char)'n') + (""+("used" + (String)"")),})
.
By default, any warning specified will be disallowed on all legal TokenTypes unless otherwise specified via the tokens property.
Also, by default warnings that are empty strings or all whitespace (regex: ^$|^\s+$) are flagged. By specifying, the format property these defaults no longer apply.
This check can be configured so that the "unchecked" and "unused" warnings cannot be suppressed on anything but variable and parameter declarations. See below of an example.
name | description | type | default value | since |
---|---|---|---|---|
format | Specify the RegExp to match against warnings. Any warning being suppressed matching this pattern will be flagged. | Pattern |
"^\s*+$"
|
5.0 |
tokens | tokens to check | subset of tokens CLASS_DEF , INTERFACE_DEF , ENUM_DEF , ANNOTATION_DEF , ANNOTATION_FIELD_DEF , ENUM_CONSTANT_DEF , PARAMETER_DEF , VARIABLE_DEF , METHOD_DEF , CTOR_DEF , COMPACT_CTOR_DEF , RECORD_DEF . | CLASS_DEF , INTERFACE_DEF , ENUM_DEF , ANNOTATION_DEF , ANNOTATION_FIELD_DEF , ENUM_CONSTANT_DEF , PARAMETER_DEF , VARIABLE_DEF , METHOD_DEF , CTOR_DEF , COMPACT_CTOR_DEF , RECORD_DEF . | 5.0 |
To configure the check:
<module name="SuppressWarnings"/>
Example:
@SuppressWarnings("") // violation class TestA { @SuppressWarnings("") // violation final int num1 = 1; @SuppressWarnings("all") // ok final int num2 = 2; @SuppressWarnings("unused") // ok final int num3 = 3; void foo1(@SuppressWarnings("unused") int param) {} // ok @SuppressWarnings("all") // ok void foo2(int param) {} @SuppressWarnings("unused") // ok void foo3(int param) {} @SuppressWarnings(true?"all":"unused") // ok void foo4(int param) {} } @SuppressWarnings("unchecked") // ok class TestB {}
To configure the check so that the "unchecked" and "unused" warnings cannot be suppressed on anything but variable and parameter declarations.
<module name="SuppressWarnings"> <property name="format" value="^unchecked$|^unused$"/> <property name="tokens" value=" CLASS_DEF,INTERFACE_DEF,ENUM_DEF, ANNOTATION_DEF,ANNOTATION_FIELD_DEF, ENUM_CONSTANT_DEF,METHOD_DEF,CTOR_DEF "/> </module>
Example:
@SuppressWarnings("") // ok class TestA { @SuppressWarnings("") // ok final int num1 = 1; @SuppressWarnings("all") // ok final int num2 = 2; @SuppressWarnings("unused") // ok final int num3 = 3; void foo1(@SuppressWarnings("unused") int param) {} // ok @SuppressWarnings("all") // ok void foo2(int param) {} @SuppressWarnings("unused") // violation void foo3(int param) {} @SuppressWarnings(true?"all":"unused") // violation void foo4(int param) {} } @SuppressWarnings("unchecked") // violation class TestB {}
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.annotation