ArrayBracketNoWhitespace

Since Checkstyle 13.10.0

Description

Checks that the whitespace around square-bracket tokens [ and ] follows the Google Java Style Guide requirements for array declarations, array creation, and array indexing as described in Section 4.6.2: Horizontal Whitespace.

Left square bracket ("["):

  • must not be preceded with whitespace when preceded by a TYPE or IDENT in 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], or arr[i];
    • postfix operators: arr[i]++ or arr[i]--
    • a right parenthesis or another closing construct: (arr[i])

Examples

To configure the check:


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

Example 1:


class Example1 {
  int[] numbersGood = new int[10];
  int[] numbersWarn = new int[10 ]; // violation ''\]' is preceded with whitespace'

  String[] dataGood = {"a", "b"};
  String [] dataWarn = {"a", "b"}; // violation ''\[' is preceded with whitespace'

  byte bufferGood[];
  byte bufferWarn[ ];
  // 2 violations above:
  // ''\[' is followed by whitespace'
  // ''\]' is preceded with whitespace'

  int[] matrixGood[];
  int[] matrixWarn[] ; // violation ''\]' is followed by whitespace'

  void methodGood(int[] arr) {}
  void methodWarn(int[]arr) {} // violation ''\]' is not followed by whitespace'

  void processArray(int[] arr) {
    int[] goodArr = {arr[0], arr[1]};
    int[] warnArr = {arr[0], arr[1] };// violation ''\]' is followed by whitespace'

    int valueGood = arr[0] * 2;
    int valueWarn = arr[0]* 2; // violation ''\]' is not followed by whitespace'

    int good = arr[0] >> 2;
    int warn = arr[0]>>2; // violation ''\]' is not followed by whitespace'

    arr[0]++;
    arr[0] ++; // violation ''\]' is followed by whitespace'

    Class<?> goodClass = int[].class;
    Class<?> warnClass = int[] .class; // violation ''\]' is followed by whitespace.'

    IntFunction<int[]> goodMethodRef = int[]::new;
    IntFunction<int[]> warnMethodRef = int[] ::new;
    // violation above ''\]' is followed by whitespace.'
    IntFunction<int[] > warnAngleBracket = int[]::new;
    // violation above ''\]' is followed by whitespace.'
  }

  void goodEllipsis(String[]... params) {}
  void warnEllipsis(String[] ... params) {}
  // violation above ''\]' is followed by 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.ArrayBracketNoWhitespaceCheck

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

Parent Module

TreeWalker