Skip to content

yield fails Indentation check in switch expressions #15967

@nickelaway

Description

@nickelaway

I have read check documentation: https://checkstyle.sourceforge.io/checks/misc/indentation.html
I have downloaded the latest cli 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

$ javac IndentationFoo.java
$
$ cat checkstyle-foo.xml 
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
        "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
    <module name="TreeWalker">
        <module name="Indentation">
            <property name="forceStrictCondition" value="true"/>
        </module>
    </module>
</module>
$ cat IndentationFoo.java 
import java.time.DayOfWeek;
import java.time.LocalDate;

public class IndentationFoo {
    public static void main(final String[] args) {
        final DayOfWeek today = LocalDate.now().getDayOfWeek();
        final boolean isWeekend = switch (today) {
            case SATURDAY:
            System.out.println("Saturday"); // Warning: expected indentation: 16
            yield true; // Warning: expected indentation: 16.
            case SUNDAY:
                System.out.println("Sunday"); // (no warning)
                yield true; // Warning: expected indentation: 12
            default:
yield false; // Warning: expected indentation: 12
        };
        System.out.println("isWeekend = " + isWeekend);
    }
}
$ java -jar checkstyle-10.20.1-all.jar -c checkstyle-foo.xml IndentationFoo.java 
Starting audit...
[ERROR] IndentationFoo.java:9:13: 'block' child has incorrect indentation level 12, expected level should be 16. [Indentation]
[ERROR] IndentationFoo.java:10:13: 'yield' has incorrect indentation level 12, expected level should be 16. [Indentation]
[ERROR] IndentationFoo.java:13:17: 'yield' has incorrect indentation level 16, expected level should be 12. [Indentation]
[ERROR] IndentationFoo.java:15:1: 'yield' has incorrect indentation level 0, expected level should be 12. [Indentation]
Audit done.
Checkstyle ends with 4 errors.

The Indentation check rejects all yield statements in switch expressions when forceStrictCondition is set to true. In the example above, it rejects anything that is not 12, saying 12 is the correct level. If you set the indentation level to 12, it rejects it saying it should be 16.

This is also inconsistent with the required indentation level for other statements, which require an indentation of 16.

I would expect 16 to be accepted as the correct indentation for all of these statements.

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