WhitespaceAfter

Since Checkstyle 3.0

Description

Checks that a token is followed by whitespace, with the exception that it does not check for whitespace after the semicolon of an empty for iterator. Use Check EmptyForIteratorPad to validate empty for iterators.

Properties

Examples

To configure the check:


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

Example:


class Example1 {
  void example() throws Exception {
    if (true) {
    } else if(false) { // violation 'not followed by whitespace'
    }

    testOne("x", "y");
    testOne("z","o"); // violation 'not followed by whitespace'

    for (int i = 0; i < 10; i++){}
    for(int i = 0; i < 10; i++){} // violation 'not followed by whitespace'

    try (InputStream ignored = System.in) {}
    try(InputStream ignored = System.in) {} // violation 'not followed by whitespace'

    try {} catch (Exception e){}
    try{} catch (Exception e) {} // violation ''try' is not followed by whitespace'

    try {} finally {}
    try {} finally{} // violation 'not followed by whitespace'

    try {} catch (Error e){} finally {}
    try {} catch (Error e){} finally{} // violation 'not followed by whitespace'

    try {} catch (Exception e){}
    try {} catch(Exception e){} // violation 'not followed by whitespace'

    synchronized (this) { }
    synchronized(this) { } // violation 'not followed by whitespace'
  }

  public String testOne(String a, String b) {
    return (a + b);
  }
  public String testTwo(String a, String b) {
    return(a + b); // violation 'not followed by whitespace'
  }

  void switchExample() {
    int a = switch ("hello") {
      case "got":
        yield (1); // ok, followed by whitespace
      case "my":
        yield(3); // violation ''yield' is not followed by whitespace'
      default:
        yield 2;
    };
  }
}

To configure the check for whitespace only after COMMA and SEMI tokens:


<module name="Checker">
  <module name="TreeWalker">
    <module name="WhitespaceAfter">
      <property name="tokens" value="COMMA, SEMI"/>
    </module>
  </module>
</module>

Example:


class Example2 {
  void example() {
    int a = 0; int b = 1;
    int c = 2;int d = 3; // violation 'not followed by whitespace'

    testMethod(a, b);
    testMethod(c,d); // violation 'not followed by whitespace'

    for(;;) {}
  }
  void testMethod(int a, int b) { }
}

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.WhitespaceAfterCheck

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

Parent Module

TreeWalker