Skip to content

checkstyle expects different indentation for switch cases. #17167

@0utplay

Description

@0utplay

Hi, this is essentially a duplicate of #17163 I just tried to make it as minimal as possible.

/var/tmp$ export JAVA_HOME=/home/roman/java/jdk/jdk-21.0.2
/var/tmp$ export PATH=$JAVA_HOME/bin:$PATH
/var/tmp$ javac Test.java 


/var/tmp $ cat config.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
  "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
  "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
  <module name="TreeWalker">
    <property name="skipFileOnJavaParseException" value="true"/>
    <property name="javaParseExceptionSeverity" value="info"/>
    <module name="Indentation">
      <property name="basicOffset" value="2"/>
      <property name="braceAdjustment" value="0"/>
      <property name="caseIndent" value="2"/>
      <property name="throwsIndent" value="2"/>
      <property name="lineWrappingIndentation" value="2"/>
      <property name="arrayInitIndent" value="2"/>
    </module>
  </module>
  <property name="fileExtensions" value="java, properties, xml"/>
  <property name="charset" value="UTF-8"/>
  <property name="severity" value="warning"/>
</module>


/var/tmp $ cat Test.java
package de.playo;

import java.lang.reflect.Proxy;

public class Test {

  public static void main(String[] args0) {
    var serviceAFakeInstance = (String) Proxy.newProxyInstance(
      null,
      new Class[]{String.class},
      (proxy, method, args) -> switch (method.getName()) {
        case "hashCode" -> 123456789;
        case "equals" -> args[0] == proxy;
        case "toString" -> "FakeInstanceServiceA";
        default -> throw new IllegalArgumentException(method.getName());
      });
    System.out.println(serviceAFakeInstance);
  }
}


$ java -jar checkstyle-10.25.0-all.jar -c config.xml Test.java
Starting audit...
[WARN] /var/tmp/Test.java:12:9: 'case' child has incorrect indentation level 8, expected level should be 10. [Indentation]
[WARN] /var/tmp/Test.java:13:9: 'case' child has incorrect indentation level 8, expected level should be 10. [Indentation]
[WARN] /var/tmp/Test.java:14:9: 'case' child has incorrect indentation level 8, expected level should be 10. [Indentation]
[WARN] /var/tmp/Test.java:15:9: 'case' child has incorrect indentation level 8, expected level should be 10. [Indentation]
[WARN] /var/tmp/Test.java:16:7: 'switch rcurly' has incorrect indentation level 6, expected level should be 8. [Indentation]
Audit done.

Describe what you expect in detail.
The indentation looks fine to me and adding two more levels does not look right. This config worked before 10.24.X

/var/tmp$ java -jar checkstyle-10.23.1-all.jar -c config.xml Test.java
Starting audit...
Audit done.

/var/tmp$ java -jar checkstyle-10.24.0-all.jar -c config.xml Test.java
Starting audit...
[WARN] /var/tmp/Test.java:12:9: 'case' child has incorrect indentation level 8, expected level should be 10. [Indentation]
[WARN] /var/tmp/Test.java:13:9: 'case' child has incorrect indentation level 8, expected level should be 10. [Indentation]
[WARN] /var/tmp/Test.java:14:9: 'case' child has incorrect indentation level 8, expected level should be 10. [Indentation]
[WARN] /var/tmp/Test.java:15:9: 'case' child has incorrect indentation level 8, expected level should be 10. [Indentation]
[WARN] /var/tmp/Test.java:16:7: 'switch rcurly' has incorrect indentation level 6, expected level should be 8. [Indentation]
Audit done.


Opensource project that uses latest versions and run into same problem
https://github.com/trinodb/trino/blob/2718456e96a386d7e0ea0b6d620918061edfcda0/.github/DEVELOPMENT.md?plain=1#L32
can be reused in CI for verification of fix.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions