Skip to content

Add Support for Java 21 Record Pattern : MissingSwitchDefault #14891

@mahfouz72

Description

@mahfouz72

child of #14890

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


PS D:\CS\test> javac src/RecordPatterns.java --enable-preview --source 22
PS D:\CS\test> cat src/RecordPatterns.java
import java.util.List;

public class RecordPatterns {
    sealed interface I permits C, D { }
    final class C implements I { }
    final class D implements I { }
    record Pair<T>(T x, T y) { }

    void test() {
        C c = new C();
        D d = new D();

        Pair<I> p2 = new Pair<>(c, d);
        // exhaustiveness check by the compiler
        switch (p2) {   // violation ??
            case Pair<I>(I i, C j) -> System.out.println("Pair (C|D, C)");
            case Pair<I>(I i, D j) -> System.out.println("Pair (C|D, D)");
        }
    }

    public static void main(String[] args) {
        new RecordPatterns().test();
    }
}
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="MissingSwitchDefault"/>
 </module>
</module>
PS D:\CS\test> java  -jar checkstyle-10.14.2-all.jar -c config.xml  src/RecordPatterns.java
Starting audit...
[ERROR] D:\CS\test\src\RecordPatterns.java:15:9: switch without "default" clause. [MissingSwitchDefault]
Audit done.
Checkstyle ends with 1 errors.

Describe what you expect in detail.

I expect no violation here since there is an exhaustiveness Check by the compiler. This means that all possible inputs must be covered. So there is no need for default


Metadata

Metadata

Assignees

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions