SuppressionSingleFilter

Since Checkstyle 8.23

Description

Filter SuppressionSingleFilter suppresses audit events for Checks violations in the specified file, class, checks, message, module id, lines, and columns.

Rationale: To allow users to use suppressions configured in the same config as other modules. SuppressionFilter and SuppressionXpathFilter require a separate file.

Advice: If checkstyle configuration is used for several projects, single suppressions on common files/folders is better to put in checkstyle configuration as common rule. All suppression that are for specific file names is better to keep in project specific config file.

Attention: This filter only supports single suppression, and will need multiple instances if users wants to suppress multiple violations.

Notes

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

Properties

name description type default value since
checks Define the RegExp for matching against the name of the check associated with an audit event. Pattern null 8.23
columns Specify a comma-separated list of values, where each value is an integer or a range of integers denoted by integer-integer. String null 8.23
files Define the RegExp for matching against the file name associated with an audit event. Pattern null 8.23
id Specify a string matched against the ID of the check associated with an audit event. String null 8.23
lines Specify a comma-separated list of values, where each value is an integer or a range of integers denoted by integer-integer. String null 8.23
message Define the RegExp for matching against the message of the check associated with an audit event. Pattern null 8.23

Examples

To configure a filter to suppress violations of JavadocStyle and MagicNumber checks in Example1.java for specific line ranges using SuppressionSingleFilter:


<module name="Checker">
  <module name="TreeWalker">
    <module name="JavadocStyle"/>
    <module name="MagicNumber"/>
  </module>
  <module name="SuppressionSingleFilter">
    <property name="checks" value="JavadocStyle|MagicNumber"/>
    <property name="files" value="Example1.java"/>
    <property name="lines" value="1,5-100"/>
  </module>
  <module name="SuppressionSingleFilter">
    <property name="message" value="Missing a Javadoc comment"/>
  </module>
</module>


public class Example1 {
  public void exampleMethod() {
    int value = 100; // filtered violation ''100' is a magic number'
  }
}

To configure a filter to suppress violations of NoWhitespaceAfter on a specific column in Example2.java using SuppressionSingleFilter:


<module name="Checker">
  <module name="TreeWalker">
    <module name="NoWhitespaceAfter"/>
  </module>
  <module name="SuppressionSingleFilter">
    <property name="files" value="Example2.java"/>
    <property name="checks" value="NoWhitespaceAfter"/>
    <property name="columns" value="12"/>
  </module>
</module>


public class Example2 {

  public void exampleMethod(int a, int b) {
    // filtered violation below ''.' is followed by whitespace'
    Integer. parseInt("3");
  }

  public void exampleMethod2() {
    int [] x; // violation ''int' is followed by whitespace'
  }

}

To configure a filter to suppress violations of RegexpSinglelineCheck in Example3.java using SuppressionSingleFilter:


<module name="Checker">
  <module name="RegexpSingleline">
    <property name="format" value="example"/>
  </module>
  <module name="SuppressionSingleFilter">
    <property name="files" value="Example3.java"/>
    <property name="checks" value="RegexpSinglelineCheck"/>
  </module>
</module>


public class Example3 {

  public void printExample() {
    System.out.println(
      "example" // filtered violation 'Line matches the illegal pattern 'example''
    );
  }

  public void noViolation() {
    System.out.println(
      "RegexpSingleline is case sensitive by default. 'Example' in not matching."
    );
  }

}

To configure a filter to suppress violations of NoWhitespaceAfter in Example4.java using SuppressionSingleFilter:


<module name="Checker">
  <module name="TreeWalker">
    <module name="MemberName">
      <property name="id" value="customMemberName"/>
    </module>
  </module>
  <module name="SuppressionSingleFilter">
    <property name="files" value="Example4.java"/>
    <property name="id" value="customMemberName"/>
  </module>
</module>


public class Example4 {
  // filtered violation below 'Name 'MyVariable' must match pattern'
  private int MyVariable = 5;
}

Example of Usage

Package

com.puppycrawl.tools.checkstyle.filters

Parent Module

Checker