Skip to content

Indentation of the Block child of switch rule is not validated when no braces #15098

@mahfouz72

Description

@mahfouz72

detected at #14997

I have read check documentation: https://checkstyle.org/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


PS D:\CS\test> cat config.xml                                                                                         
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
        "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
    <property name="charset" value="UTF-8"/>
    <module name="TreeWalker">
            <module name="Indentation"/>
    </module>
</module>
PS D:\CS\test> cat src/Test.java                                                                                      
public class Test {

    void test(int obj) {
        switch (obj) {
            case 1 ->
System.out.println("1");                      // This is not being validated
            case 2 -> {
System.out.println("2");  // violation        (there are braces)
            }
        }

        switch (obj) {
            case 1 :
System.out.println("1");  // violation
            case 2 : {
System.out.println("2");  // violation
            }
        }

        if (obj == 1)
System.out.println("1");  // violation
        else {
System.out.println("2");  // violation
        }
    }

}
PS D:\CS\test> java  -jar checkstyle-10.17.0-all.jar -c config.xml src/Test.java
Starting audit...
[ERROR] D:\CS\test\src\Test.java:8:1: 'block' child has incorrect indentation level 0, expected level should be 16. [Indentation]
[ERROR] D:\CS\test\src\Test.java:14:1: 'block' child has incorrect indentation level 0, expected level should be 16. [Indentation]
[ERROR] D:\CS\test\src\Test.java:16:1: 'block' child has incorrect indentation level 0, expected level should be 16. [Indentation]
[ERROR] D:\CS\test\src\Test.java:21:1: 'if' child has incorrect indentation level 0, expected level should be 12. [Indentation]
[ERROR] D:\CS\test\src\Test.java:23:1: 'else' child has incorrect indentation level 0, expected level should be 12. [Indentation]
Audit done.
Checkstyle ends with 5 errors.
PS D:\CS\test> 


Describe what you want in detail.

I expect the block child of the switch rule to be validated even if there are no braces.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions