SingleSpaceSeparator

Since Checkstyle 6.19

Description

Checks that non-whitespace characters are separated by no more than one whitespace. Separating characters by tabs or multiple spaces will be reported. Currently, the check doesn't permit horizontal alignment. To inspect whitespaces before and after comments, set the property validateComments to true.

Setting validateComments to false will ignore cases like:

int i;  // Multiple whitespaces before comment tokens will be ignored.
private void foo(int  /* whitespaces before and after block-comments will be
ignored */  i) {
        

Sometimes, users like to space similar items on different lines to the same column position for easier reading. This feature isn't supported by this check, so both braces in the following case will be reported as violations.

public long toNanos(long d)  { return d;             } // 2 violations
public long toMicros(long d) { return d / (C1 / C0); }
        

Properties

name description type default value since
validateComments Control whether to validate whitespaces surrounding comments. boolean false 6.19

Examples

To configure the check:

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

Example:

class Example1 {
  int foo()   { // violation 'Use a single space'
    return  1; // violation 'Use a single space'
  }
  int fun1() {
    return 3;
  }
  void  fun2() {} // violation 'Use a single space'
}
        

To configure the check so that it validates comments:

<module name="Checker">
  <module name="TreeWalker">
    <module name="SingleSpaceSeparator">
      <property name="validateComments" value="true"/>
    </module>
  </module>
</module>
        

Example:

class Example2 {
  // violation below 'Use a single space'
  void fun1() {}  // 2 whitespaces before the comment starts
  // violation below 'Use a single space'
  void fun2() { return; }  /* 2 whitespaces before the comment starts */
  // violation below 'Use a single space'
  /* 2 whitespaces after the comment ends */  int a;

  String s; /* OK, 1 whitespace */

  /**
   * This is a Javadoc comment
   */  int b; // 2 whitespaces after the javadoc comment ends
  // violation above 'Use a single space'
  float f1;

  /**
   * OK, 1 white space after the doc comment ends
   */ float f2;
}
        

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.

Package

com.puppycrawl.tools.checkstyle.checks.whitespace

Parent Module

TreeWalker