Skip to content

WhitespaceAround: new property allowEmptySwitchBlockStatements #9540

Closed
@mrclmh

Description

I have read check documentation: https://checkstyle.org/config_xxxxxx.html#NameOfAffectedCheck
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 YOUR_FILE.java
#[[MAKE SURE THERE IS SUCCESSFUL COMPILATION]]

/var/tmp $ cat config.xml

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
        "http://checkstyle.sourceforge.net/dtds/configuration_1_3.dtd">

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
        "http://checkstyle.sourceforge.net/dtds/configuration_1_3.dtd">

<module name="Checker">
    <property name="charset" value="UTF-8"/>
    <property name="haltOnException" value="true"/>
    <property name="severity" value="error"/>

    <module name="FileTabCharacter">
        <property name="eachLine" value="true"/>
    </module>
    <module name="NewlineAtEndOfFile"/>
    <module name="SuppressWarningsFilter"/>
    <module name="UniqueProperties"/>

    <module name="TreeWalker">
        <module name="SuppressWarningsHolder"/>

        <module name="WhitespaceAround">
            <property name="allowEmptyCatches" value="true"/>
            <property name="allowEmptyConstructors" value="true"/>
            <property name="allowEmptyMethods" value="true"/>
            <property name="allowEmptyLambdas" value="true"/>
            <property name="allowEmptyLoops" value="true"/>
            <property name="allowEmptyTypes" value="true"/>
            <property name="ignoreEnhancedForColon" value="false"/>
        </module>
    </module>
</module>

/var/tmp $ cat YOUR_FILE.java

public class WhitespaceAround {
    private void switchTest() {
        switch ("foo") {
            case "A" -> {}
            case "B" -> System.out.println("B");
            default -> System.out.println("other");
        }
    }
}

/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java $RUN_LOCALE -jar checkstyle-X.XX-all.jar -c config.xml YOUR_FILE.java

Starting audit...
[ERROR] /private/var/tmp/WhitespaceAround.java:4:25: '{' is not followed by whitespace. [WhitespaceAround]
[ERROR] /private/var/tmp/WhitespaceAround.java:4:26: '}' is not preceded with whitespace. [WhitespaceAround]
Audit done.
Checkstyle ends with 2 errors.

For Windows users, please use type instead of cat and run

set RUN_LOCALE="-Duser.language=en -Duser.country=US"
java %RUN_LOCALE% -jar checkstyle-X.XX-all.jar -c config.xml YOUR_FILE.java

in place of the last 2 commands above.


My expectation was that one of the allowEmpty* properties would stop the violation on the line

case "A" -> {}

Solution: is new property allowEmptySwitchBlockStatements to let users control behavior on empty blocks.

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions