google/google-java-format

Comments before 'case' of 'switch' make formating weird

romani opened this issue · 1 comments

/var/tmp$ cat Test.java 
public class Test {
  void method(int i, int j, boolean cond) {
    while (true) {
      switch (i) {
        case 0: // no problem
        case 1:
          i++;
          break;
        case 2:
          i++;
        /*fall through*/ case 3:
          i++;
          break;
      }
    }
  }
}

/var/tmp$ java -jar google-java-format-1.22.0-all-deps.jar Test.java > TestUpdated.java

diff:

/var/tmp$ diff -u Test.java TestUpdated.java 
--- Test.java	2024-07-26 21:52:16.871580578 -0700
+++ TestUpdated.java	2024-07-26 21:52:24.399697339 -0700
@@ -8,7 +8,7 @@
           break;
         case 2:
           i++;
-        /*fall through*/ case 3:
+          /*fall through*/ case 3:
           i++;
           break;
       }

result file is weirdly formatted for case 3:

rivanov@p5510:/var/tmp$ cat TestUpdated.java 
public class Test {
  void method(int i, int j, boolean cond) {
    while (true) {
      switch (i) {
        case 0: // no problem
        case 1:
          i++;
          break;
        case 2:
          i++;
          /*fall through*/ case 3:
          i++;
          break;
      }
    }
  }
}

This was recently fixed in f7543b2. That change will be included in the next release of the formatter.

After that change, it emits:

public class Test {
  void method(int i, int j, boolean cond) {
    while (true) {
      switch (i) {
        case 0: // no problem
        case 1:
          i++;
          break;
        case 2:
          i++;
        /*fall through*/ case 3:
          i++;
          break;
      }
    }
  }
}