Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Check Support for Java 21 Pattern Matching for Switch Syntax: NoWhitespaceBeforeCaseDefaultColon #15048

Open
mahfouz72 opened this issue Jun 20, 2024 · 1 comment
Assignees
Labels

Comments

@mahfouz72
Copy link
Member

child of #14961

I have read check documentation:https://checkstyle.org/checks/whitespace/nowhitespacebeforecasedefaultcolon.html
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 src/Test.java                                                
record ColoredPoint(int p, int x, boolean c) { }
record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) { }

public class Test {
    void test(Object obj) {   // NPath = 6
        switch (obj) {
            case ColoredPoint(int a, int b, _) when (a >= b) : {} break; // violation
            case ColoredPoint(int a, int b, _) when (b > 100) : {} break; // violation
            case ColoredPoint(int a, int b, _) when (b != 1000) : {} break; // violation
            case ColoredPoint(int a, int b, _) when (b != 100000) : {} break; // violation
            case Rectangle(_,_) : {} // violation
            default : System.out.println("none"); // violation
        };
    }
}
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="NoWhitespaceBeforeCaseDefaultColon">
        </module>
    </module>
</module>
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:7:62: ':' is preceded with whitespace. [NoWhitespaceBeforeCaseDefaultColon]
[ERROR] D:\CS\test\src\Test.java:8:63: ':' is preceded with whitespace. [NoWhitespaceBeforeCaseDefaultColon]
[ERROR] D:\CS\test\src\Test.java:9:65: ':' is preceded with whitespace. [NoWhitespaceBeforeCaseDefaultColon]
[ERROR] D:\CS\test\src\Test.java:10:67: ':' is preceded with whitespace. [NoWhitespaceBeforeCaseDefaultColon]
[ERROR] D:\CS\test\src\Test.java:11:33: ':' is preceded with whitespace. [NoWhitespaceBeforeCaseDefaultColon]
[ERROR] D:\CS\test\src\Test.java:12:21: ':' is preceded with whitespace. [NoWhitespaceBeforeCaseDefaultColon]
Audit done.
Checkstyle ends with 6 errors.
PS D:\CS\test> 


This check is working as expected.

@nrmancuso
Copy link
Member

Let's create a new test case in this check, it doesn't have to be super involved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants