Skip to content

Indentation: google-java-format and Checkstyle disagree about switch block indentation #3899

@jplace

Description

@jplace

https://checkstyle.org/config_misc.html#Indentation

[jplace@Jordans-MacBook-Pro checkstyle-playground]$ cat src/main/java/Main.java

public class Main {
  /**
   * Main for testing.
   *
   * @param args Program arguments
   */
  public static void main(String[] args) {
    Test myTest = Test.ONE;

    switch (myTest) {
      case ONE:
        {
          System.out.println("One");
          break;
        }
      case TWO:
        {
          System.out.println("Two");
          break;
        }
      case THREE:
        {
          System.out.println("Three");
          break;
        }
      default:
        throw new RuntimeException("Unexpected value");
    }
  }

  public enum Test {
    ONE,
    TWO,
    THREE
  }
}

[jplace@Jordans-MacBook-Pro checkstyle-playground]$ cat checkstyle.xml

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
          "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">

<module name = "Checker">
    <module name="TreeWalker">
        <module name="Indentation">
            <property name="basicOffset" value="2"/>
            <property name="braceAdjustment" value="0"/>
            <property name="caseIndent" value="2"/>
            <property name="throwsIndent" value="4"/>
            <property name="lineWrappingIndentation" value="4"/>
            <property name="arrayInitIndent" value="2"/>
        </module>
    </module>
</module>

I've distilled this from the Google Checkstyle configuration for the purposes of file this bug:
https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml

[jplace@Jordans-MacBook-Pro checkstyle-playground]$ mvn checkstyle:checkstyle

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building checkstyle-playground 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-checkstyle-plugin:2.17:checkstyle (default-cli) @ checkstyle-playground ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Starting audit...
[ERROR] /Users/jplace/Documents/work/checkstyle-playground/src/main/java/Main.java:12: 'block lcurly' have incorrect indentation level 8, expected level should be 6. [Indentation]
[ERROR] /Users/jplace/Documents/work/checkstyle-playground/src/main/java/Main.java:13: 'block' child have incorrect indentation level 10, expected level should be 8. [Indentation]
[ERROR] /Users/jplace/Documents/work/checkstyle-playground/src/main/java/Main.java:14: 'block' child have incorrect indentation level 10, expected level should be 8. [Indentation]
[ERROR] /Users/jplace/Documents/work/checkstyle-playground/src/main/java/Main.java:15: 'block rcurly' have incorrect indentation level 8, expected level should be 6. [Indentation]
[ERROR] /Users/jplace/Documents/work/checkstyle-playground/src/main/java/Main.java:17: 'block lcurly' have incorrect indentation level 8, expected level should be 6. [Indentation]
[ERROR] /Users/jplace/Documents/work/checkstyle-playground/src/main/java/Main.java:18: 'block' child have incorrect indentation level 10, expected level should be 8. [Indentation]
[ERROR] /Users/jplace/Documents/work/checkstyle-playground/src/main/java/Main.java:19: 'block' child have incorrect indentation level 10, expected level should be 8. [Indentation]
[ERROR] /Users/jplace/Documents/work/checkstyle-playground/src/main/java/Main.java:20: 'block rcurly' have incorrect indentation level 8, expected level should be 6. [Indentation]
[ERROR] /Users/jplace/Documents/work/checkstyle-playground/src/main/java/Main.java:22: 'block lcurly' have incorrect indentation level 8, expected level should be 6. [Indentation]
[ERROR] /Users/jplace/Documents/work/checkstyle-playground/src/main/java/Main.java:23: 'block' child have incorrect indentation level 10, expected level should be 8. [Indentation]
[ERROR] /Users/jplace/Documents/work/checkstyle-playground/src/main/java/Main.java:24: 'block' child have incorrect indentation level 10, expected level should be 8. [Indentation]
[ERROR] /Users/jplace/Documents/work/checkstyle-playground/src/main/java/Main.java:25: 'block rcurly' have incorrect indentation level 8, expected level should be 6. [Indentation]
Audit done.
[INFO] There are 12 errors reported by Checkstyle 7.6 with checkstyle.xml ruleset.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.313 s
[INFO] Finished at: 2017-03-02T14:47:16-08:00
[INFO] Final Memory: 18M/309M
[INFO] ------------------------------------------------------------------------

[jplace@Jordans-MacBook-Pro checkstyle-playground]$ cat pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>checkstyle-playground</groupId>
    <artifactId>checkstyle-playground</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <!-- Java Compiler -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- Check Java code style -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>2.17</version>
                <!--
                Force Maven to use latest version of checkstyle
                -->
                <dependencies>
                    <dependency>
                        <groupId>com.puppycrawl.tools</groupId>
                        <artifactId>checkstyle</artifactId>
                        <version>7.6</version>
                    </dependency>
                </dependencies>
                <!--
                Bind checkstyle to run as part of Maven validate phase
                -->
                <executions>
                    <execution>
                        <id>checkstyle</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <configLocation>checkstyle.xml</configLocation>
                    <violationSeverity>info</violationSeverity>
                    <logViolationsToConsole>true</logViolationsToConsole>
                    <failOnViolation>true</failOnViolation>
                    <consoleOutput>true</consoleOutput>
                    <linkXRef>false</linkXRef>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

I'm using google-java-format to format Main.java. It disagrees with Google's Checkstyle configuration for how this code should be indented. I'm not sure which tool is authority here.

Am I correct in thinking they should agree?


Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions