Skip to content

indentation mismatch with Google formatter over code blocks under switch #14294

@NathanEckert

Description

@NathanEckert

I have read check documentation:

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

/var/tmp $ javac mymodule/src/main/java/com/example/checkstyle/CheckstyleScope.java

/var/tmp $ cat minimal-rules.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC
  "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
  "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
  <module name="TreeWalker">
    <module name="LeftCurly">
      <property name="tokens"
        value="ANNOTATION_DEF, CLASS_DEF, CTOR_DEF, ENUM_CONSTANT_DEF, ENUM_DEF,
                      INTERFACE_DEF, LAMBDA, LITERAL_CATCH, LITERAL_DEFAULT,
                      LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY, LITERAL_FOR, LITERAL_IF,
                      LITERAL_SWITCH, LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE, METHOD_DEF,
                      OBJBLOCK, STATIC_INIT, RECORD_DEF, COMPACT_CTOR_DEF"/>
    </module>
    <module name="Indentation">
      <property name="basicOffset" value="2"/>
      <property name="braceAdjustment" value="2"/>
      <property name="caseIndent" value="2"/>
      <property name="throwsIndent" value="4"/>
      <property name="lineWrappingIndentation" value="4"/>
      <property name="arrayInitIndent" value="2"/>
    </module>
  </module>
</module>

/var/tmp $ cat mymodule/src/main/java/com/example/checkstyle/CheckstyleScope.java
package com.example.checkstyle;

public class CheckstyleScope {
  private static void indentationIssue(Condition condition) {
    switch (condition) {
      case TRUE:
        {
          System.out.println("True");
          break;
        }
      case FALSE:
        {
          System.out.println("False");
          break;
        }
      default:
        System.out.println("Other");
    }

    {
      System.out.println("True");
    }
  }

  private enum Condition {
    TRUE,
    FALSE
  }
}


/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java $RUN_LOCALE -jar ~/Downloads/checkstyle-10.12.7-all.jar -c ./minimal-rules.xml mymodule/src/main/java/com/example/checkstyle/CheckstyleScope.java
Starting audit...
[ERROR] /home/path/mymodule/src/main/java/com/example/checkstyle/CheckstyleScope.java:14:9: 'block lcurly' has incorrect indentation level 8, expected level should be 6. [Indentation]
[ERROR] /home/path/mymodule/src/main/java/com/example/checkstyle/CheckstyleScope.java:15:11: 'block' child has incorrect indentation level 10, expected level should be 8. [Indentation]
[ERROR] /home/path/mymodule/src/main/java/com/example/checkstyle/CheckstyleScope.java:16:11: 'block' child has incorrect indentation level 10, expected level should be 8. [Indentation]
[ERROR] /home/path/mymodule/src/main/java/com/example/checkstyle/CheckstyleScope.java:17:9: 'block rcurly' has incorrect indentation level 8, expected level should be 6. [Indentation]
[ERROR] /home/path/mymodule/src/main/java/com/example/checkstyle/CheckstyleScope.java:19:9: 'block lcurly' has incorrect indentation level 8, expected level should be 6. [Indentation]
[ERROR] /home/path/mymodule/src/main/java/com/example/checkstyle/CheckstyleScope.java:20:11: 'block' child has incorrect indentation level 10, expected level should be 8. [Indentation]
[ERROR] /home/path/mymodule/src/main/java/com/example/checkstyle/CheckstyleScope.java:21:11: 'block' child has incorrect indentation level 10, expected level should be 8. [Indentation]
[ERROR] /home/path/mymodule/src/main/java/com/example/checkstyle/CheckstyleScope.java:22:9: 'block rcurly' has incorrect indentation level 8, expected level should be 6. [Indentation]
Audit done.
Checkstyle ends with 8 errors.

If I modify the rules to change to <property name="braceAdjustment" value="0"/>,
I manage to get ride of some errors but new ones appears:

/var/tmp $ java -jar ~/Downloads/checkstyle-10.12.7-all.jar -c ./minimal-rules.xml mymodule/src/main/java/com/example/checkstyle/CheckstyleScope.java 
Starting audit...
[ERROR] /home/path/mymodule/src/main/java/com/example/checkstyle/CheckstyleScope.java:27:5: 'block lcurly' has incorrect indentation level 4, expected level should be 6. [Indentation]
[ERROR] /home/path/mymodule/src/main/java/com/example/checkstyle/CheckstyleScope.java:29:5: 'block rcurly' has incorrect indentation level 4, expected level should be 6. [Indentation]
Audit done.
Checkstyle ends with 2 errors.

The file was previously formatted with google-java-format (https://github.com/google/google-java-format/releases/tag/v1.19.2):

/var/tmp $ java -jar ~/Downloads/google-java-format-1.19.2-all-deps.jar --replace mymodule/src/main/java/com/example/checkstyle/CheckstyleScope.java

Might be linked with #3612, #3899 and #9326

Metadata

Metadata

Assignees

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions