RightCurly

Since Checkstyle 3.0

Description

Checks the placement of right curly braces ('}') for code blocks. This check supports if-else, try-catch-finally blocks, switch statements, while-loops, for-loops, method definitions, class definitions, constructor definitions, instance, static initialization blocks, annotation definitions and enum definitions. For right curly brace of expression blocks of arrays, lambdas and class instances please follow issue #5945. For right curly brace of enum constant please follow issue #7519.

Properties

Examples

To configure the check:

<module name="RightCurly"/>
        

Example:

public class Test {

  public void test() {

    if (foo) {
      bar();
    }           // violation, right curly must be in the same line as the 'else' keyword
    else {
      bar();
    }

    if (foo) {
      bar();
    } else {     // OK
      bar();
    }

    if (foo) { bar(); } int i = 0;  // violation
                  // ^^^ statement is not allowed on same line after curly right brace

    if (foo) { bar(); }            // OK
    int i = 0;

    try {
      bar();
    }           // violation, rightCurly must be in the same line as 'catch' keyword
    catch (Exception e) {
      bar();
    }

    try {
      bar();
    } catch (Exception e) { // OK
      bar();
    }

  }                         // OK

  public void testSingleLine() { bar(); } // OK, because singleline is allowed
}
        

To configure the check with policy alone for else and METHOD_DEF tokens:

<module name="RightCurly">
  <property name="option" value="alone"/>
  <property name="tokens" value="LITERAL_ELSE, METHOD_DEF"/>
</module>
        

Example:

public class Test {

  public void test() {

    if (foo) {
      bar();
    } else { bar(); }   // violation, right curly must be alone on line

    if (foo) {
      bar();
    } else {
      bar();
    }                   // OK

    try {
      bar();
    } catch (Exception e) { // OK because config is set to token METHOD_DEF and LITERAL_ELSE
      bar();
    }

  }                         // OK

  public void violate() { bar; } // violation, singleline is not allowed here

  public void ok() {
    bar();
  }                              // OK
}
        

To configure the check with policy alone for Switch Statements:

<module name="RightCurly">
  <property name="option" value="alone"/>
  <property name="tokens" value="LITERAL_SWITCH"/>
</module>
        
class Test {

    public void method0() {
        int mode = 0;
        switch (mode) {
            case 1:
                int x = 1;
                break;
            default:
                x = 0;
        } // ok, RightCurly is alone
    }

    public void method0() {
        int mode = 0;
        switch (mode) {
            case 1:
                int x = 1;
                break;
            default:
                x = 0; } // violation, RightCurly should be alone on a line
    }

}
        

To configure the check with policy alone_or_singleline for if and METHOD_DEF tokens:

<module name="RightCurly">
  <property name="option" value="alone_or_singleline"/>
  <property name="tokens" value="LITERAL_IF, METHOD_DEF"/>
</module>
        

Example:

public class Test {

  public void test() {

    if (foo) {
      bar();
    } else {        // violation, right curly must be alone on line
      bar();
    }

    if (foo) {
      bar();
    }               // OK
    else {
      bar();
    }

    try {
      bar();
    } catch (Exception e) {        // OK because config did not set token LITERAL_TRY
      bar();
    }

  }                                // OK

  public void violate() { bar(); } // OK , because singleline
}
        

To configure the check with policy alone_or_singleline for Switch Statements:

<module name="RightCurly">
  <property name="option" value="alone_or_singleline"/>
  <property name="tokens" value="LITERAL_SWITCH"/>
</module>
        
class Test {

    public void method0() {
        int mode = 0;
        switch (mode) {
            case 1:
                int x = 1;
                break;
            default:
                x = 0;
        } // ok
    }

    public static void method7() {
        int mode = 0;
        int x;
        switch (mode) { case 1: x = 5; } // ok, RightCurly is on the same line as LeftCurly
    }

    public void method() {
        int mode = 0;
        int x;
        switch (mode) {
            case 1:
                x = 1; } // violation, right curly should be alone on line
    }
}
        

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