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

Created on 2 Mar 2017  路  9Comments  路  Source: checkstyle/checkstyle

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?




approved bug google style has bounty high demand indentation

Most helpful comment

I am on it.

All 9 comments

They should agree with each other in theory. I believe that google-java-format is the authority, since it's developed by Google engineers and Checkstyle has been trying to match up with google-java-format AFAICT. But I'm sure someone from the Checkstyle team can go into further detail...

I'm not sure which tool is authority here.

Authority is questionable, as google style guide is incomplete in their coverage of all code user can write. This document is in progress of development.
google-java-format - is tool from Google, and author of it have more connections to Google's seniors who can change guide. But it also could mis-interpret Google Guide.
checkstyle - is thirparty and indepent project that could cover guide and do this to level we could. We are not ideal and do have bugs, the same as google-java-format.

If smth is not clearly written in Guide we usually ask Google to clarify by opening issue on them - https://github.com/google/styleguide/issues .

We change smth in checkstyle code and config only if that is become written at https://google.github.io/styleguide/javaguide.html , if not written so it is not a rule, even smb interpret some words n different way...

This is a known inconsistency in the style guide.

搂4.1.2 says "blocks and block-like constructs [should have] no line break before the opening brace.", which suggests:

case FOO: {
  // indented +2
}

搂4.8.4.1 says "After a switch label, a newline appears, and the indentation level is increased +2,", which would mean:

case FOO:
  {
    // indented +4
  }

The owners of the style guide are aware of this issue and are planning to clarify the style guide eventually. In the mean time, they have indicated that google-java-format's interpretation of the style guide is OK in the situation.

this issue caused used to disable Check completely in google config.
See https://github.com/checkstyle/checkstyle/issues/6946#issuecomment-519687605

Our Check should be fixed as explanation from cushon is valid. Location of { is unusual for me but .... it is valid.

I am on it.

I am on it.

good! let us know when released!

is this fixed?

@anshupitlia Not yet! We are finding some robust solution. It will be fixed soon with some discussion with mentors.

Fix is merged

Was this page helpful?
0 / 5 - 0 ratings