Skip to content

Line break must be present after { of non-empty block in switch rule #17565

@mohitsatr

Description

@mohitsatr

I have read check documentation: https://checkstyle.org/checks/misc/indentation.html
I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words

From: https://google.github.io/styleguide/javaguide.html#s4.8.4-switch

In a new-style switch, a switch rule can be written on a single line if it otherwise follows Google style. (It must not exceed the column limit, and if it contains a non-empty block then there must be a line break after {.)

Code:

/** some javadoc. */
public class Test {

  void testMethod1(int month) {
    int result =
        switch (month) {
          case 1, 6, 7 -> {
            System.out.println("try");
            yield 1;
          }
          case 2, 9, 10, 11, 12 -> { yield 2; }  // Expected violation
          case 3, 5, 4, 8 -> { yield month * 4; }  // Expected violation
          default -> 0;
        };
  }

  void testMethod2(int number) {
    switch (number) {
      case 0, 1 -> System.out.println("0");
      case 2 ->
          handleTwoWithAnExtremelyLongMethodCallThatWouldNotFitOnTheSameLine();
      default -> { handleSurprisingNumber(number); } // Expected violation
    }
  }

  private void handleSurprisingNumber(int number) {
    number++;
  }

  private int handleTwoWithAnExtremelyLongMethodCallThatWouldNotFitOnTheSameLine() {
    return 0;
  }
}

Cli:

$ java -jar checkstyle-10.26.1-all.jar -c google_checks.xml Test.java 
Starting audit...
Audit done.

Formatter:

$ java -jar google-java-format-1.27.0-all-deps.jar Test.java > FormattedCode.java
$ diff -Naru Test.java FormattedCode.java
--- Test.java   2025-08-06 15:15:12.435365500 +0530
+++ FormattedCode.java  2025-08-06 15:15:41.022134100 +0530
@@ -8,8 +8,12 @@
             System.out.println("try");
             yield 1;
           }
-          case 2, 9, 10, 11, 12 -> { yield 2; }
-          case 3, 5, 4, 8 -> { yield month * 4; }
+          case 2, 9, 10, 11, 12 -> {
+            yield 2;
+          }
+          case 3, 5, 4, 8 -> {
+            yield month * 4;
+          }
           default -> 0;
         };
   }
@@ -17,9 +21,10 @@
   void testMethod2(int number) {
     switch (number) {
       case 0, 1 -> System.out.println("0");
-      case 2 ->
-          handleTwoWithAnExtremelyLongMethodCallThatWouldNotFitOnTheSameLine();
-      default -> { handleSurprisingNumber(number); }
+      case 2 -> handleTwoWithAnExtremelyLongMethodCallThatWouldNotFitOnTheSameLine();
+      default -> {
+        handleSurprisingNumber(number);
+      }
     }
   }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions