ArrayBracketNoWhitespace
Since Checkstyle 13.1.0
Description
Checks that the whitespace around square-bracket tokens
[ and ]
follows the typical Java convention for array declarations, array creation,
and array indexing.
Left square bracket ("["):
- must not be preceded with whitespace when preceded by a
TYPEorIDENTin array declarations or array access - must not be followed with whitespace
Right square bracket ("]"):
- must not be preceded with whitespace
- must be followed with whitespace in all cases, except when followed by:
- another bracket:
[][] - a dot for member access:
arr[i].length - a comma or semicolon:
arr[i],orarr[i]; - postfix operators:
arr[i]++orarr[i]-- - a right parenthesis or another closing construct:
(arr[i])
- another bracket:
Examples
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="ArrayBracketNoWhitespace"/>
</module>
</module>
Examples with incorrect array bracket whitespace:
class Example1 {
int array1[] ; // violation, space before ;
int array2[ ] = new int[ 5 ]; // 3 violations, space in [] and before ;
int[] array3 = new int[5 ]; // violation, space before ]
int array4 [] = new int [ 5 ] ;
// 5 violations, space after variable name, in all []
int array5
[ ]
=
new int
[ 1 ] ; // violations, space in []
void method1(int param[ ]) { // violation, space in []
}
void method2() {
int arr [ ] = { 1, 2, 3 }; // violations, spaces in []
}
}
Examples without violations:
class Example2 {
int[] array1;
int[] array2 = new int[5];
int[] array3 = new int[]{1, 2, 3};
void method1(int[] param) {
}
void method2() {
int[] arr = { 1, 2, 3 };
}
}
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






