SuppressWarnings

Since Checkstyle 5.0

Description

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.

Properties

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

Examples

To configure the check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="SuppressWarnings"/>
  </module>
</module>
        

Example:

// violation below 'The warning '' cannot be suppressed at this location'
@SuppressWarnings("")
class Example1 {
  // violation below 'The warning '' cannot be suppressed at this location'
  @SuppressWarnings("")
  final int num1 = 1;
  // ok below as default format only checks for a blank or empty string
  @SuppressWarnings("all")
  final int num2 = 2;
  // ok below as default format only checks for a blank or empty string
  @SuppressWarnings("unused")
  final int num3 = 3;

  void foo1(@SuppressWarnings("unused") int param) {}

  @SuppressWarnings("all")
  void foo2(int param) {}
  @SuppressWarnings("unused")
  void foo3(int param) {}
  @SuppressWarnings(true?"all":"unused")
  void foo4(int param) {}
}
// ok below as default format only checks for a blank or empty string
@SuppressWarnings("unchecked")
class Test1 {}
        

To configure the check so that the "unchecked" and "unused" warnings cannot be suppressed on anything but variable and parameter declarations.

<module name="Checker">
  <module name="TreeWalker">
    <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>
  </module>
</module>
        

Example:

// ok below, since we are only checking for '^unchecked$|^unused$'
@SuppressWarnings("")
class Example2 {
  // ok below as VARIABLE_DEF is not configured in tokens to check
  @SuppressWarnings("")
  final int num1 = 1;
  @SuppressWarnings("all")
  final int num2 = 2;
  @SuppressWarnings("unused")
  final int num3 = 3;

  // ok below as PARAMETER_DEF is not configured in tokens to check
  void foo1(@SuppressWarnings("unused") int param) {}

  // ok below, since we are only checking for '^unchecked$|^unused$'
  @SuppressWarnings("all")
  void foo2(int param) {}

  // violation below, 'The warning 'unused' cannot be suppressed at this location'
  @SuppressWarnings("unused")
  void foo3(int param) {}

  // violation below, 'The warning 'unused' cannot be suppressed at this location'
  @SuppressWarnings(true?"all":"unused")
  void foo4(int param) {}
}

// violation below, 'The warning 'unchecked' cannot be suppressed at this location'
@SuppressWarnings("unchecked")
class Test2 {}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.annotation

Parent Module

TreeWalker