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: VariableDeclarationUsageDistance #15043

Open
mahfouz72 opened this issue Jun 19, 2024 · 1 comment

Comments

@mahfouz72
Copy link
Member

child of #14961

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

\test> cat src/Test.java                                                
record ColoredPoint(int p, int x, String c) {
}

record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) {
}

public class Test {

    void test(Object obj) {
        switch (obj) {
            case ColoredPoint(int x, _, _) -> {
                System.out.println("line1");
                System.out.println("line2");
                System.out.println("line3");
                System.out.println("line4");
                System.out.println(x);  // 5
            }
            default -> System.out.println("none");
        }

        if (obj instanceof ColoredPoint(int x, _, _)) {
            System.out.println("line1");
            System.out.println("line2");
            System.out.println("line3");
            System.out.println("line4");
            System.out.println(x);
        }
        int x;
        System.out.println("line1");
        System.out.println("line2");
        System.out.println("line3");
        System.out.println("line4");
        x = 1;   // 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="VariableDeclarationUsageDistance">

        </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:28:9: Distance between variable 'x' declaration and its first usage is 5, but allowed 3.  Consider making that variable final if you still need to store its value in advance (before method calls that might have side effects on the original value). [VariableDeclarationUsageDistance]
Audit done.
Checkstyle ends with 1 errors.
PS D:\CS\test> 


Describe what you want in detail.

I don't think that this check should apply to pattern variables. We have no choice in the pattern variable declaration position. They are being declared either in ifs or case labels.

@nrmancuso
Copy link
Member

I am approving this for us to create some interesting inputs to make sure that we are good here, and to prevent future regressions.

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