GoogleRightCurly

Since Checkstyle 14.2.0

Description

Checks the placement of right curly braces ('}') for code blocks, following the Google Java Style Guide .

For nonempty blocks the right curly brace must begin its own line, unless it is followed by else, catch, finally, or a comma, in which case no line break follows it.

For empty blocks, either K&R style or the concise {} form is allowed, except within a multi-block statement (if/else, try/catch/finally).

Examples

To configure the check:


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

Example:


public class Example1 {
  static {
    a = 0;
  } static int a = 1;
  // violation above 'should be alone on a line'
  void method() {
    if (a == 0) {
      bar(1, 2, 3);
      // violation below 'should be on the same line'
    }
    else {
      bar(1, 2, 3);
    }
    try {
      bar(-1, -2);
      // violation below 'should be on the same line'
    }
    catch(Exception e) {
      bar(0);
    }
    do {
      bar(a);
      a--;
      // violation below 'should be on the same line'
    }
    while (a > 0);
  }
  void method2() {
    switch (a) {
      case 1: int y = 1; break;
      case 2: {bar(-1);} // violation 'should be alone on a line'
      case 3: {break;}   // violation 'should be alone on a line'
      default: {
        bar(-2);
      }
    }
    // violation below 'should have line break before'
    if (a == 0) {bar(a);} else {
      bar(a--);
    }
  }
  void bar(int... n)  {int[] k = n;}
  // violation above 'should be alone on a line'
  void foo() {  }
  // violation above 'Empty block should be concise {}'
  void foo1() {} static class bar1 {}
  // violation above 'should have line break after'
}

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

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

Parent Module

TreeWalker