WhitespaceBeforeEmptyBody

Since Checkstyle 13.9.0

Description

Checks that a whitespace is present before the opening brace of an empty body.

This check applies to the following tokens: Methods and constructors, Classes, interfaces, enums, and records, Annotation definitions, Loops: while, for, do-while, Lambdas and anonymous classes, Static initializer blocks, Try-catch-finally statements, Synchronized blocks, Switch statements.

A body containing only comments is considered empty by this check.

Properties

Examples

To configure the check:


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

Example:


class Example1 {

  void method(){}  // violation ''{' is not preceded with whitespace'

  void method2(){  // violation ''{' is not preceded with whitespace'
    // comment
  }

  void method3() {
    for (int i = 0; i < 1; i++){}
    // violation above ''{' is not preceded with whitespace'
  }

  class Internal{} // violation ''{' is not preceded with whitespace'

}

To configure the check for only methods and classes:


<module name="Checker">
  <module name="TreeWalker">
    <module name="WhitespaceBeforeEmptyBody">
          <property name="tokens"
                value="METHOD_DEF, CLASS_DEF"/>
     </module>
  </module>
</module>

Example:


class Example2 {

  void method(){}  // violation ''{' is not preceded with whitespace'

  void method2(){  // violation ''{' is not preceded with whitespace'
    // comment
  }

  void method3() {
    for (int i = 0; i < 1; i++){}

  }

  class Internal{} // violation ''{' is not preceded with whitespace'

}

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.

Fully Qualified Name

com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceBeforeEmptyBodyCheck

Use this fully qualified class name in configuration when an exact class reference is required.

Parent Module

TreeWalker