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

The following suppressions directs a SuppressionSingleFilter to reject JavadocStyleCheck violations for lines 82 and 108 to 122 of file AbstractComplexityCheck.java, and MagicNumberCheck violations for line 221 of file JavadocStyleCheck.java, and 'Missing a Javadoc comment' violations for all lines and files:


<module name="Checker">
  <module name="TreeWalker">
    <module name="JavadocStyle"/>
    <module name="MagicNumber"/>
  </module>
  <module name="SuppressionSingleFilter">
    <property name="checks" value="JavadocStyle"/>
    <property name="files" value="Example1.java"/>
    <property name="lines" value="1,5-100"/>
  </module>
  <module name="SuppressionSingleFilter">
    <property name="checks" value="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'
  }
}

Suppress check by module id when config have two instances on the same check:


<module name="Checker">
  <module name="EqualsAvoidNull"/>
  <module name="JavadocMethod"/>
  <module name="SuppressionSingleFilter">
    <property name="id" value="stringEqual"/>
    <property name="files" value="Example2.java"/>
    <property name="checks" value="EqualsAvoidNull, JavadocMethod"/>
  </module>
</module>


public class Example2 {

  public void checkStringEquality(String str1, String str2) {
    // filtered violation ''.equals()' should be used for string comparison'
    assert str1 == str2 ;
  }
}

Suppress all checks for hidden files and folders:


<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() {
    // filtered violation 'Line matches the illegal pattern 'example''
    System.out.println(
      "This is an example string."
    );
  }

  public void noViolation() {
    System.out.println(
      "This string does not contain 'example'."
    );
  }

}

Suppress all checks for Maven-generated code:


<module name="Checker">
  <module name="NoWhitespaceAfter"/>
  <module name="SuppressionSingleFilter">
    <property name="files" value="Example4.java"/>
    <property name="checks" value="NoWhitespaceAfter"/>
  </module>
</module>


public class Example4 {

  // filtered violation 'WhiteSpace after ',''
  public void exampleMethod(int a, int b) {
  }

  public void exampleMethod2() {
    int x = 5 ; // filtered violation 'WhiteSpace before ';''
  }

}

Suppress all checks for archives, classes and other binary files:


<module name="Checker">
  <module name="MethodName"/>
  <module name="SuppressionSingleFilter">
    <property name="files" value="Example5.java"/>
    <property name="checks" value="MethodName"/>
  </module>
</module>


public class Example5 {
  // filtered violation 'Name 'example_Method' must match pattern'
  public void example_Method() {
  }

  // filtered violation Name 'Another_Method' must match pattern
  public void Another_Method() {
  }
}

Suppress all checks for image files:


<module name="Checker">
  <module name="ConstantName"/>
  <module name="SuppressionSingleFilter">
    <property name="files" value="Example6.java"/>
    <property name="checks" value="ConstantName"/>
  </module>
</module>


public class Example6 {

  // filtered violation 'Name 'myConstant' must match pattern'
  private static final int myConstant = 42;

}

Suppress all checks for non-java files:


<module name="Checker">
  <module name="MemberName"/>
  <module name="SuppressionSingleFilter">
    <property name="files" value="Example7.java"/>
    <property name="checks" value="MemberName"/>
  </module>
</module>


public class Example7 {

  // filtered violation 'Name 'MyVariable' must match pattern'
  private int MyVariable = 5;

  // filtered violation 'Name 'PrintHello' must match pattern'
  public void PrintHello() {
  }

  public void printHello() {
  }
}

Suppress all checks in generated sources:


<module name="Checker">
  <module name="TreeWalker">
    <module name="ParameterNumber">
      <property name="max" value="5"/>
    </module>
  </module>
  <module name="SuppressionSingleFilter">
    <property name="files" value="Example8.java"/>
    <property name="checks" value="ParameterNumber"/>
  </module>
</module>


public class Example8 {
  // filtered violation 'more than 5 parameters'
  public void exampleMethod(
    int param1, int param2, int param3, int param4,
    int param5
  ) {}
}

Suppress FileLength check on integration tests in certain folder:


<module name="Checker">
  <module name="TreeWalker">
    <module name="FileLength">
      <property name="max" value="1"/>
    </module>
  </module>
  <module name="SuppressionSingleFilter">
    <property name="files" value="Example9.java"/>
    <property name="checks" value="FileLength"/>
  </module>
</module>


public class Example9 {
  //filtered violation 'File length is 19 lines (max allowed is 1)'
}

Suppress naming violations on variable named 'log' in all files:


<module name="Checker">
  <module name="MemberName">
    <property name="format" value="^[A-Z][a-zA-Z0-9]*$"/>
  </module>
  <module name="SuppressionSingleFilter">
    <property name="files" value="Example10.java"/>
    <property name="message" value="Name 'log' must match pattern"/>
  </module>
</module>


public class Example10 {

  // filtered violation 'Name 'log' must match pattern'
  private String log = "Some log message";

}

Example of Usage

Package

com.puppycrawl.tools.checkstyle.filters

Parent Module

Checker