checkstyle / checkstyle

Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. By default it supports the Google Java Style Guide and Sun Code Conventions, but is highly configurable. It can be invoked with an ANT task and a command line program.
https://checkstyle.org
GNU Lesser General Public License v2.1
8.22k stars 3.63k forks source link

Add Check Support for Java 21 Pattern Matching for Switch Syntax: NPathComplexity #15047

Open mahfouz72 opened 1 week ago

mahfouz72 commented 1 week ago

child of https://github.com/checkstyle/checkstyle/issues/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


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="NPathComplexity">
            <property name="max" value="1"/>
        </module>
    </module>
</module>
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, _) 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");
        };
    }
}
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:5:5: NPath Complexity is 6 (max allowed is 1). [NPathComplexity]
[ERROR] D:\CS\test\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 (->)

nrmancuso commented 1 week ago

Looks like a false negative, approving this one.

rnveach commented 1 week ago

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.