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: NPathComplexity #15047

Open
mahfouz72 opened this issue Jun 20, 2024 · 3 comments
Assignees
Labels
approved false negative issues where check should place violations on code, but does not

Comments

@mahfouz72
Copy link
Member

mahfouz72 commented Jun 20, 2024

child of #14961

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


c 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="NPathComplexity">
            <property name="max" value="1"/>
        </module>
    </module>
</module>
➜  src cat 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, _) when (a >= b) : {} break;
            case ColoredPoint(int a, int b, _) when (b > 100) : {} break;
            case ColoredPoint(int a, int b, _) when (b != 1000) : {} break;
            case ColoredPoint(int a, int b, _) when (b != 100000) : {} break;
            case Rectangle(_,_) : {}
            default : System.out.println("none");
        };
    }

    void test2(Object obj) {
        switch (obj) {
            case ColoredPoint(int a, int b, _) when (a >= b) -> {}
            case ColoredPoint(int a, int b, _) when (b > 100) -> {}
            case ColoredPoint(int a, int b, _) when (b != 1000) -> {}
            case ColoredPoint(int a, int b, _) when (b != 100000) -> {}
            case Rectangle(_,_) -> {}
            default -> System.out.println("none");
        };
    }
}
➜  src javac --enable-preview --release 21 Test.java                        
Note: Test.java uses preview features of Java SE 21.
Note: Recompile with -Xlint:preview for details.
➜  src java -jar checkstyle-10.18.1-SNAPSHOT-all.jar -c config.xml Test.java
Starting audit...
[ERROR] /home/nick/IdeaProjects/tester/src/Test.java:5:5: NPath Complexity is 6 (max allowed is 1). [NPathComplexity]
[ERROR] /home/nick/IdeaProjects/tester/src/Test.java:16:5: NPath Complexity is 2 (max allowed is 1). [NPathComplexity]
Audit done.
Checkstyle ends with 2 errors.



Describe what you want in detail

I expect both methods to have the same NPathComplexity. There is a problem in counting when case labels are in the switch rule (->)

@mahfouz72 mahfouz72 changed the title dd Check Support for Java 21 Pattern Matching for Switch Syntax: NPathComplexity Add Check Support for Java 21 Pattern Matching for Switch Syntax: NPathComplexity Jun 20, 2024
@nrmancuso
Copy link
Member

Looks like a false negative, approving this one.

@rnveach
Copy link
Member

rnveach commented Jun 21, 2024

https://checkstyle.org/checks/metrics/npathcomplexity.html#Description

Expressions Number of && and || operators in expression. No operators - 0

This check also counts expressions, so fixing when may cause other effects here.

@nrmancuso nrmancuso assigned nrmancuso and unassigned mahfouz72 Jun 21, 2024
@nrmancuso
Copy link
Member

OP is updated, same result as before, false negative on -> syntax.

@nrmancuso nrmancuso added false negative issues where check should place violations on code, but does not approved labels Jul 9, 2024
@nrmancuso nrmancuso assigned mahfouz72 and unassigned nrmancuso Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved false negative issues where check should place violations on code, but does not
Projects
None yet
Development

No branches or pull requests

3 participants