NeedBraces

Since Checkstyle 3.0

Description

Checks for braces around code blocks.

Attention: The break in case blocks is not counted to allow compact view.

Properties

name description type default value since
allowEmptyLoopBody Allow loops with empty bodies. boolean false 6.12.1
allowSingleLineStatement Allow single-line statements without braces. boolean false 6.5
tokens tokens to check subset of tokens LITERAL_DO , LITERAL_ELSE , LITERAL_FOR , LITERAL_IF , LITERAL_WHILE , LITERAL_CASE , LITERAL_DEFAULT , LAMBDA . LITERAL_DO , LITERAL_ELSE , LITERAL_FOR , LITERAL_IF , LITERAL_WHILE . 3.0

Examples

To configure the check:

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

Example:

class Example1 {
  String obj = new String();
  String value = new String();
  int counter = 1;
  int count = 0;
  int num = 12;
  String o = "O";
  public boolean Example1() {
    if (obj.equals(num)) return true;
    // violation above, ''if' construct must use '{}'s.'
    if (true) {
      count = 2;
    } else
        // violation above, ''else' construct must use '{}'s.'
        return false;
    for (int i = 0; i < 5; i++) {
      ++count;}
    do // violation, ''do' construct must use '{}'s.'
        ++count;
    while (false);
    for (int j = 0; j < 10; j++);
    // violation above, ''for' construct must use '{}'s.'
    for(int i = 0; i < 10; value.charAt(12));
    // violation above, ''for' construct must use '{}'s.'
    while (counter < 10)
        // violation above, ''while' construct must use '{}'s.'
        ++count;
    while (value.charAt(12) < 5);
    // violation above, ''while' construct must use '{}'s.'
    switch (num) {
      case 1: counter++; break;
      // ok above, because break in case blocks is not counted to allow compact view
    }
    return true;
  }
}
        

To configure the check for if and else blocks:

<module name="Checker">
  <module name="TreeWalker">
    <module name="NeedBraces">
      <property name="tokens" value="LITERAL_IF, LITERAL_ELSE"/>
    </module>
  </module>
</module>
        

Example:

class Example2 {
  String obj = new String();
  String value = new String();
  int counter = 1;
  int count = 0;
  int num = 12;
  String o = "O";
  public boolean Example2() {
    if (obj.equals(num)) return true;
    // violation above, ''if' construct must use '{}'s.'
    if (true) {
      count = 2;
    } else
        // violation above, ''else' construct must use '{}'s.'
        return false;
    for (int i = 0; i < 5; i++) {
      ++count;}
    do // ok, because DO is not a target of validation
        ++count;
    while (false);
    for (int j = 0; j < 10; j++);
    // ok above, because FOR is not a target of validation
    for(int i = 0; i < 10; value.charAt(12));
    // ok above, because FOR is not a target of validation
    while (counter < 10)
        // ok above, because WHILE is not a target of validation
        ++count;
    while (value.charAt(12) < 5);
    // ok above, because WHILE is not a target of validation
    switch (num) {
      case 1: counter++; break;
      // ok above, because break in case blocks is not counted to allow compact view
    }
    return true;
  }
}
        

To configure the check to allow single-line statements (if, while, do-while, for) without braces:

<module name="Checker">
  <module name="TreeWalker">
    <module name="NeedBraces">
      <property name="allowSingleLineStatement" value="true"/>
      <property name="tokens"
             value="LITERAL_IF, LITERAL_WHILE, LITERAL_DO, LITERAL_FOR"/>
    </module>
  </module>
</module>
        

Example:

class Example3 {
  String obj = new String();
  String value = new String();
  int counter = 1;
  int count = 0;
  int num = 12;
  String o = "O";
  public boolean Example3() {
    if (obj.equals(num)) return true;
    // ok above, because single line IF statement is allowed in config.
    if (true) {
      count = 2;
    } else
        // ok above, because single line ELSE statement is allowed in config.
        return false;
    for (int i = 0; i < 5; i++) {
      ++count;}
    do // violation, ''do' construct must use '{}'s.'
        ++count;
    while (false);
    for (int j = 0; j < 10; j++);
    // ok above, because single line FOR statement is allowed in config.
    for(int i = 0; i < 10; value.charAt(12));
    // ok above, because single line FOR statement is allowed in config.
    while (counter < 10)
        // violation above, ''while' construct must use '{}'s.'
        ++count;
    while (value.charAt(12) < 5);
    // ok above, because single line FOR statement is allowed in config.
    switch (num) {
      case 1: counter++; break;
      // ok above, because break in case blocks is not counted to allow compact view
    }
    return true;
  }
}
        

To configure the check to allow case, default single-line statements without braces:

<module name="Checker">
  <module name="TreeWalker">
    <module name="NeedBraces">
      <property name="tokens" value="LITERAL_CASE, LITERAL_DEFAULT"/>
      <property name="allowSingleLineStatement" value="true"/>
    </module>
  </module>
</module>
        

Next statements won't be violated by check:

class Example4 {
  String obj = new String();
  String value = new String();
  int counter = 1;
  int count = 0;
  int num = 12;
  String o = "O";
  public boolean Example4() {

    if (obj.equals(num)) return true;
    // ok above, because IF is not a target of validation
    if (true) {
      count = 2;
    } else
        // ok above, because ELSE is not a target of validation
        return false;
    for (int i = 0; i < 5; i++) {
      ++count;}
    do // ok, because DO is not a target of validation
        ++count;
    while (false);
    for (int j = 0; j < 10; j++);
    // ok above, because FOR is not a target of validation
    for(int i = 0; i < 10; value.charAt(12));
    // ok above, because FOR is not a target of validation
    while (counter < 10)
        // ok above, because WHILE is not a target of validation
        ++count;
    while (value.charAt(12) < 5);
    // ok above, because WHILE is not a target of validation
    switch (num) {
      case 1: counter++; break;
      // ok above, because break in case blocks is not counted to allow compact view
    }
    return true;
  }
}
        

To configure the check to allow loops (while, for) with empty bodies:

<module name="Checker">
  <module name="TreeWalker">
    <module name="NeedBraces">
      <property name="allowEmptyLoopBody" value="true"/>
      <property name="tokens" value="LITERAL_WHILE, LITERAL_FOR"/>
    </module>
  </module>
</module>
        

Example:

class Example5 {
  String obj = new String();
  String value = new String();
  int counter = 1;
  int count = 0;
  int num = 12;
  String o = "O";
  public boolean Example5() {
    if (obj.equals(num)) return true;
    // ok above, because IF is not a target of validation
    if (true) {
      count = 2;
    } else
        // ok above, because ELSE is not a target of validation
        return false;
    for (int i = 0; i < 5; i++) {
      ++count;}
    do // ok, because DO is not a target of validation
        ++count;
    while (false);
    for (int j = 0; j < 10; j++);
    // ok above, because FOR is not a target of validation
    for(int i = 0; i < 10; value.charAt(12));
    // ok above, because FOR is not a target of validation
    while (counter < 10)
        // violation above, ''while' construct must use '{}'s.'
        ++count;
    while (value.charAt(12) < 5);
    // ok above, because WHILE is not a target of validation
    switch (num) {
      case 1: counter++; break;
      // ok above, because break in case blocks is not counted to allow compact view
    }
    return true;
  }
}
        

To configure the check to lambdas:

<module name="Checker">
  <module name="TreeWalker">
    <module name="NeedBraces">
      <property name="tokens" value="LAMBDA"/>
      <property name="allowSingleLineStatement" value="true"/>
    </module>
  </module>
</module>
        

Results in following:

class Example6 {
  enum HttpMethod {GET, OPTIONS}
  Object result = new Object();
  private CustomCompletableFuture<Object> allowedFuture;
  Example6() {
    allowedFuture = new CustomCompletableFuture<>();
    allowedFuture.addCallback(result -> assertEquals("Invalid response", // violation
                    EnumSet.of(HttpMethod.GET, HttpMethod.OPTIONS), result),
            ex -> fail(ex.getMessage()));
    allowedFuture.addCustomCallback(() -> {
      return assertEquals("Invalid response",
              EnumSet.of(HttpMethod.GET, HttpMethod.OPTIONS), result);},
            ex -> fail(ex.getMessage()));}
  private Object assertEquals(String invalidResponse, EnumSet<HttpMethod> get,
                              Object result) {
    return result;}
  private String fail(String message) {
    return message;
  }}
class CustomCompletableFuture<T> {
  private CompletableFuture<T> allowedFuture;
  public CustomCompletableFuture() {
    allowedFuture = new CompletableFuture<>();
  }
  public void addCallback(java.util.function.Consumer<T> successCallback,
          java.util.function.Consumer<Throwable> failureCallback) {}
  public void addCustomCallback(java.util.function.Supplier<Object> successLambda,
          java.util.function.Consumer<Throwable> failureLambda) {}
}
        

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

Parent Module

TreeWalker