NoWhitespaceBefore
Since Checkstyle 3.0
Description
Checks that there is no whitespace before a token. More
specifically, it checks that it is not preceded with whitespace, or
(if linebreaks are allowed) all characters on the line before are
whitespace. To allow linebreaks before a token, set property
allowLineBreaks
to true
. No check occurs
before semicolons in empty for loop initializers or conditions.
Properties
name | description | type | default value | since |
---|---|---|---|---|
allowLineBreaks | Control whether whitespace is allowed if the token is at a linebreak. | boolean | false |
3.0 |
tokens | tokens to check | subset of tokens COMMA , SEMI , POST_INC , POST_DEC , DOT , GENERIC_START , GENERIC_END , ELLIPSIS , LABELED_STAT , METHOD_REF . | COMMA , SEMI , POST_INC , POST_DEC , ELLIPSIS , LABELED_STAT . | 3.0 |
Examples
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="NoWhitespaceBefore"/>
</module>
</module>
Example:
class Example1 {
int foo = 5;
void example() {
foo ++; // violation 'is preceded with whitespace'
foo++;
for (int i = 0 ; i < 5; i++) {} // violation '';' is preceded with whitespace'
for (int i = 0; i < 5; i++) {}
int[][] array = { { 1, 2 }
, { 3, 4 } }; // violation '',' is preceded with whitespace'
int[][] array2 = { { 1, 2 },
{ 3, 4 } };
Lists.charactersOf("foo").listIterator()
.forEachRemaining(System.out::print)
; // violation '';' is preceded with whitespace'
{
label1 : // violation '':' is preceded with whitespace'
for (int i = 0; i < 10; i++) {}
}
{
label2:
while (foo < 5) {}
}
}
}
To configure the check to allow linebreaks before default tokens:
<module name="Checker">
<module name="TreeWalker">
<module name="NoWhitespaceBefore">
<property name="allowLineBreaks" value="true"/>
</module>
</module>
</module>
Example:
class Example2 {
int[][] array = { { 1, 2 }
, { 3, 4 } };
int[][] array2 = { { 1, 2 },
{ 3, 4 } };
void ellipsisExample(String ...params) {};
// violation above ''...' is preceded with whitespace'
void ellipsisExample2(String
...params) {
Lists.charactersOf("foo")
.listIterator()
.forEachRemaining(System.out::print);
};
}
To Configure the check to restrict the use of whitespace before METHOD_REF and DOT tokens:
<module name="Checker">
<module name="TreeWalker">
<module name="NoWhitespaceBefore">
<property name="tokens" value="METHOD_REF, DOT"/>
</module>
</module>
</module>
Example:
class Example3 {
void example() {
Lists.charactersOf("foo").listIterator()
.forEachRemaining(System.out::print);
// violation above ''.' is preceded with whitespace'
Lists.charactersOf("foo").listIterator().forEachRemaining(System.out ::print);
// violation above ''::' is preceded with whitespace'
Lists.charactersOf("foo").listIterator().forEachRemaining(System.out::print);
}
}
To configure the check to allow linebreak before METHOD_REF and DOT tokens:
<module name="Checker">
<module name="TreeWalker">
<module name="NoWhitespaceBefore">
<property name="tokens" value="METHOD_REF, DOT"/>
<property name="allowLineBreaks" value="true"/>
</module>
</module>
</module>
Example:
class Example4 {
void example() {
Lists .charactersOf("foo") // violation ''.' is preceded with whitespace'
.listIterator()
.forEachRemaining(System.out ::print);
// violation above ''::' is preceded with whitespace'
Lists.charactersOf("foo")
.listIterator()
.forEachRemaining(System.out::print);
}
}
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