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: CommentsIndentation #15049

Open
mahfouz72 opened this issue Jun 20, 2024 · 2 comments

Comments

@mahfouz72
Copy link
Member

child of #14961

I have read check documentation: https://checkstyle.org/checks/misc/commentsindentation.html#CommentsIndentation
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) {
        switch (obj) {
            case ColoredPoint(int a, int b, _)
                   // comment 1
                    when (a >= b) :
                 // comment 2
                {} break;
            case ColoredPoint(int a, int b, _) when (b > 100) // comment 3
                    :
                     /* violation
                     *
                     */
            {}
             // ok
            break;
            default : System.out.println("none"); // violation
        }
         switch (obj) {
            case ColoredPoint(int a, int b, _)
                   // comment 1
                    when (a >= b)
                            // comment 2
                    -> {}
            case ColoredPoint(int a, int b, _) when (b > 100) // comment 3
                    ->
                     /* comment 4
                     *
                     */
            {}
             // ok
            default -> System.out.println("none");
        };
    }
}
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="CommentsIndentation">
        </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:8:20: Comment has incorrect indentation level 19, expected is 20, indentation should be the same level as line 9. [CommentsIndentation]
[ERROR] D:\CS\test\src\Test.java:10:18: Comment has incorrect indentation level 17, expected is 16, indentation should be the same level as line 11. [CommentsIndentation]      
[ERROR] D:\CS\test\src\Test.java:14:22: Block comment has incorrect indentation level 21, expected is 12, indentation should be the same level as line 17. [CommentsIndentation]
[ERROR] D:\CS\test\src\Test.java:18:14: Comment has incorrect indentation level 13, expected is 12, indentation should be the same level as line 19. [CommentsIndentation]      
[ERROR] D:\CS\test\src\Test.java:24:20: Comment has incorrect indentation level 19, expected is 20, indentation should be the same level as line 25. [CommentsIndentation]      
[ERROR] D:\CS\test\src\Test.java:26:29: Comment has incorrect indentation level 28, expected is 20, indentation should be the same level as line 27. [CommentsIndentation]      
[ERROR] D:\CS\test\src\Test.java:30:22: Block comment has incorrect indentation level 21, expected is 12, indentation should be the same level as line 33. [CommentsIndentation]
[ERROR] D:\CS\test\src\Test.java:34:14: Comment has incorrect indentation level 13, expected is 12, 12, indentation should be the same level as line 33, 35. [CommentsIndentation]
Audit done.
Checkstyle ends with 8 errors.
PS D:\CS\test> 

We are ok here. This is working as intended.

@nrmancuso
Copy link
Member

Let's add this example as a new test case for this check, there is lots of weird stuff here that might prove useful during regression testing.

@rnveach
Copy link
Member

rnveach commented Jun 21, 2024

When we add inputs, we should have bad and properly formatted examples.

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

No branches or pull requests

3 participants