Skip to content

False positives for google_checks when assigning variable to enhanced switch expression #15592

@Edarke

Description

@Edarke

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

Here is a simplified example of code formatted according to the latest google-java-format binary, which produces violations according to the built-in google_checks.xml

~% java -jar Downloads/google-java-format-1.23.0-all-deps.jar --replace Example.java
~% javac Example.java 
~% cat Example.java 
/** Checkstyle false positive on enhanced switch. */
public class Example {

  /** Main method. */
  public static void main(String[] args) {
    String s =
        switch (1) {
          case 1 -> "Test";
          default -> "Default";
        };
  }
}

~% java $RUN_LOCALE -jar checkstyle-10.18.1-all.jar -c /google_checks.xml Example.java
Starting audit...
[WARN] /Users/evan.darke/Example.java:7:9: 'switch' has incorrect indentation level 8,
        expected level should be 4. [Indentation]
[WARN] /Users/evan.darke/Example.java:8:11: 'case' child has incorrect indentation level 10,
      expected level should be 6. [Indentation]
[WARN] /Users/evan.darke/Example.java:9:11: 'case' child has incorrect indentation level 10,
      expected level should be 6. [Indentation]
[WARN] /Users/evan.darke/Example.java:10:9: 'switch rcurly' has incorrect indentation level 8,
       expected level should be 4. [Indentation]
Audit done.

Manually "fixing" the indentation from 8 to 4 causes checkstyle to complain that it should be changed back to 8.

~% cat Example2.java && java $RUN_LOCALE -jar checkstyle-10.18.1-all.jar -c /google_checks.xml Example2.java
/** Checkstyle false positive on enhanced switch. */
public class Example2 {

  /** Main method. */
  public static void main(String[] args) {
    String s =
    switch (1) {
      case 1 -> "Test";
      default -> "Default";
    };
  }
}
Starting audit...
[WARN] /Users/evan.darke/Example2.java:7:5: 'switch' has incorrect indentation level 4,
       expected level should be 8. [Indentation]
Audit done.

Seemingly, the only way to avoid a violation is to not line wrap before the switch, but that doesn't pass google-java-format

cat Example2.java && java $RUN_LOCALE -jar checkstyle-10.18.1-all.jar -c /google_checks.xml Example2.java
/** Checkstyle false positive on enhanced switch. */
public class Example2 {

  /** Main method. */
  public static void main(String[] args) {
    String s = switch (1) {
      case 1 -> "Test";
      default -> "Default";
    };
  }
}
Starting audit...
Audit done.
~% java -jar Downloads/google-java-format-1.23.0-all-deps.jar Example2.java 
/** Checkstyle false positive on enhanced switch. */
public class Example2 {

  /** Main method. */
  public static void main(String[] args) {
    String s =
        switch (1) {
          case 1 -> "Test";
          default -> "Default";
        };
  }
}

I expect google-java-format output to not produce any violations according to google_checks.xml.


Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions