Skip to content

False-positive in Indentation check with yield switch statements #19709

@archer-321

Description

@archer-321

I have read the check documentation: https://checkstyle.org/checks/misc/indentation.html
I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the CLI and pasted its output below, as the output describes the problem better than 1,000 words.

$ javac Example.java

$ cat example-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">
        <module name="Indentation"/>
    </module>
</module>

$ cat Example.java

class Example {
    void foo() {
        int i = 0;
        int j = switch (i) {
            case 0 -> {
                yield switch (i) {
                    case 0 -> 0;  // error: expected level should be 24
                    default -> 1; // error: expected level should be 24
                };                // error: expected level should be 20
            }
            default -> 1;
        };
    }
}
$ RUN_LOCALE="-Duser.language=en -Duser.country=US"
$ java $RUN_LOCALE -jar checkstyle-13.4.0-all.jar -c example-config.xml Example.java
Starting audit...
[ERROR] /tmp/Example.java:7:21: 'case' child has incorrect indentation level 20, expected level should be 24. [Indentation]
[ERROR] /tmp/Example.java:8:21: 'case' child has incorrect indentation level 20, expected level should be 24. [Indentation]
[ERROR] /tmp/Example.java:9:17: 'switch rcurly' has incorrect indentation level 16, expected level should be 20. [Indentation]
Audit done.
Checkstyle ends with 3 errors.

Expected behaviour:
The snippet

yield switch (i) {
    case 0 -> 0;
    default -> 1;
};

should already be formatted correctly. Instead, Checkstyle expects the snippet to be formatted as

yield switch (i) {
        case 0 -> 0;
        default -> 1;
    };

I tried checking for duplicate issues; in particular, this seems similar to #17167 and #15967.
However, considering both issues are closed, I went ahead and opened a new one.

Thank you for maintaining this project!

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