SuppressWarningsFilter

Since Checkstyle 5.7

Description

Filter SuppressWarningsFilter uses annotation @SuppressWarnings to suppress audit events.

Rationale: Same as for SuppressionCommentFilter. In the contrary to it here, comments are not used comments but the builtin syntax of @SuppressWarnings. This can be perceived as a more elegant solution than using comments. Also, this approach maybe supported by various IDE.

Usage: This filter only works in conjunction with a SuppressWarningsHolder, since that check finds the annotations in the Java files and makes them available for the filter. Because of that, a configuration that includes this filter must also include SuppressWarningsHolder as a child module of the TreeWalker. Name of check in annotation is case-insensitive and should be written with any dotted prefix or "Check" suffix removed.

Notes

SuppressWarningsFilter can suppress Checks that have Treewalker or Checker as parent module.

Examples

Example of suppression by name of Check in annotation:

<module name="Checker">
  <module name="TreeWalker">
    <module name="SuppressWarningsHolder" />
    <module name="MemberName" />
    <module name="NoWhitespaceAfter" />
  </module>
  <module name="SuppressWarningsFilter" />
</module>
        

Java code:

public class Example1 {
  @SuppressWarnings("memberName")
  int J; // ok only because there is annotation
  int JJ; // violation 'must match pattern'

  @SuppressWarnings({"MemberName", "NoWhitespaceAfter"})
  int [] ARRAY; // ok only because there is annotation for both Checks
  int [] ARRAY2;
  // 2 violations above:
  //   ''int' is followed by whitespace'
  //   'must match pattern'
}
        

It is possible to specify an ID of checks, so that it can be leveraged by the SuppressWarningsFilter to skip validations. The following examples show how to skip validations near code that has @SuppressWarnings("checkstyle:<ID>") or just @SuppressWarnings("<ID>") annotation, where ID is the ID of checks you want to suppress.

Example of suppression by prefix "checkstyle:":

<module name="Checker">
  <module name="TreeWalker">
    <module name="SuppressWarningsHolder" />
    <module name="RegexpSinglelineJava">
      <property name="id" value="systemout"/>
      <property name="format" value="^.*System\.(out|err).*$"/>
      <property name="message"
        value="Don't use System.out/err, use SLF4J instead."/>
    </module>
  </module>
  <module name="SuppressWarningsFilter" />
</module>
        

Java code:

package com.puppycrawl.tools.checkstyle.filters.suppresswarningsfilter;

public class Example2 {
  @SuppressWarnings("checkstyle:systemout")
  public static void foo() {
    System.out.println("Debug info."); // ok, because there is annotation
  }
  public static void boo() {
    System.out.println("Some info."); // violation 'use SLF4J instead.'
  }
}
        

Example of Usage

Package

com.puppycrawl.tools.checkstyle.filters

Parent Module

Checker