-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Description
I have read check documentation: https://checkstyle.sourceforge.io/checks/misc/indentation.html#Indentation
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 Example.java
/var/tmp $ java -jar ~/Downloads/google-java-format-1.26.0-all-deps.jar --replace Example.java; cat Example.java
import java.util.Optional;
public class Example {
/** Main method. */
public static String main(Optional<Integer> opt) {
return opt.map(
n ->
switch (n) {
case 1 -> "Test";
default -> "Default";
})
.orElse("");
}
}
/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java -jar ~/Downloads/google-java-format-1.26.0-all-deps.jar --replace Example.java; cat Example.java; java $RUN_LOCALE -jar ~/Downloads/checkstyle-10.23.0-all.jar -c /google_checks.xml Example.java
Starting audit...
[WARN] /Users/evan.darke/Documents/IntellijProjects/Mongot2/Example.java:3:1: Missing a Javadoc comment. [MissingJavadocType]
[WARN] /Users/evan.darke/Documents/IntellijProjects/Mongot2/Example.java:9:17: 'switch' has incorrect indentation level 16, expected level should be one of the following: 6, 8, 12. [Indentation]
[WARN] /Users/evan.darke/Documents/IntellijProjects/Mongot2/Example.java:10:19: 'case' child has incorrect indentation level 18, expected level should be one of the following: 8, 10, 14. [Indentation]
[WARN] /Users/evan.darke/Documents/IntellijProjects/Mongot2/Example.java:11:19: 'case' child has incorrect indentation level 18, expected level should be one of the following: 8, 10, 14. [Indentation]
[WARN] /Users/evan.darke/Documents/IntellijProjects/Mongot2/Example.java:12:17: 'switch rcurly' has incorrect indentation level 16, expected level should be one of the following: 6, 8, 12. [Indentation]
Audit done.
This expands on #15592, which partially fixed behavior for switch expressions.
I know google_java_format is maintained separately than Checkstyle. I know there are multiple interpretations of google java style. I know there's no guarantee that these tools are compatible. However, I also think the output prescribed by Checkstyle (shown below) in this particular case is objectively an incorrect interpretation of google style. The switch expression is a continuation of the previous line and should be indented.
import java.util.Optional;
public class Example {
/** Main method. */
public static String main(Optional<Integer> opt) {
return opt.map(
n ->
switch (n) {
case 1 -> "Test";
default -> "Default";
})
.orElse("");
}
}
Currently, I'm using the following workaround, but it's not ideal as it broadly disables the check.
<module name="SuppressionXpathSingleFilter">
<property name="files" value=".*.java"/>
<property name="checks" value="Indent"/>
<property name="query"
value="//(ASSIGN | EXPR)/LITERAL_SWITCH
| //(ASSIGN | EXPR)/LITERAL_SWITCH/SWITCH_RULE/LITERAL_CASE
| //(ASSIGN | EXPR)/LITERAL_SWITCH/SWITCH_RULE/LITERAL_DEFAULT
| //(ASSIGN | EXPR)/LITERAL_SWITCH/SWITCH_RULE/SLIST//*
| //(ASSIGN | EXPR)/LITERAL_SWITCH/RCURLY "/>
</module>