Skip to content

False positive in MissingSwitchDefault with pattern in switch label #11220

@nrmancuso

Description

@nrmancuso

After #11100 (comment):

I have read check documentation: https://checkstyle.sourceforge.io/config_coding.html#MissingSwitchDefault
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

➜  company javac --enable-preview --release 17 Test.java                    
Note: Test.java uses preview features of Java SE 17.
Note: Recompile with -Xlint:preview for details.

➜  cat 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="MissingSwitchDefault">
        </module>
    </module>
</module>

➜  cat Test.java                                                    
public class Test {
    static void test4(Object o) {
        switch (o) { // ok
            case null, String s -> System.out.println("String, including null");
            default -> System.out.println("something else");
        }

        switch (o) { // ok
            case null, String s: System.out.println("String, including null"); break;
            default: System.out.println("something else");
        }

        // The following switch statements are equivalent:

        switch(o) {
            case null: default: // ok
                System.out.println("The rest (including null)");
        }

        switch(o) { // false positive, should be ok
            case null, default ->
                System.out.println("The rest (including null)");
        }

        switch(o) { // false positive, should be ok
            case null, default:
                throw new UnsupportedOperationException("not supported!");
        }
    }
}

➜  java -jar checkstyle-9.3-SNAPSHOT-all.jar -c config.xml Test.java
Starting audit...
[ERROR] Test.java:20:9: switch without "default" clause. [MissingSwitchDefault]
[ERROR] Test.java:25:9: switch without "default" clause. [MissingSwitchDefault]
Audit done.
Checkstyle ends with 2 errors.

I would expect Checkstyle to find no violations in this code example.

Metadata

Metadata

Assignees

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