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: NestedIfDepth #15036

Open
mahfouz72 opened this issue Jun 19, 2024 · 5 comments

Comments

@mahfouz72
Copy link
Member

child of #14961

I have read check documentation:https://checkstyle.org/checks/coding/nestedifdepth.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, String c) { }
record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) { }

public class Test {

    void test(Object obj) {
        switch (obj) {
            case ColoredPoint(int x, int y, String z) when (x >= 2)
                    -> {
                if (x >= 2) {
                    if(y >= 2) {
                        if (z.equals("red")) {

                        }
                    }
                }
            }
            default -> {}
        }
    }
}
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="NestedIfDepth">

        </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:12:25: Nested if-else depth is 2 (max allowed is 1). [NestedIfDepth]
Audit done.
Checkstyle ends with 1 errors.


Describe what you want in detail

when can't be nested and nested ifs in the switch blocks work correctly. We are ok here. This can be closed.

@romani
Copy link
Member

romani commented Jun 19, 2024

@nrmancuso , @rnveach , I hope in scope of this issue we extend Inputs with such code.
to make sure that " We are ok here. This can be closed." will always be OK.
If we are doubt on how it works we should find such case in Inputs , if not found in Inputs - we need to extend inputs.
It will triple prove that it works, and will stay to work forver.

@mahfouz72 , you can probably instead of opening issues , just open PR to extend Inputs, merge of it will mean that we are ok with behavior.

@nrmancuso
Copy link
Member

you can probably instead of opening issues , just open PR to extend Inputs, merge of it will mean that we are ok with behavior.

Problem with this is that users (and us) won't find this in the future. "Shouldn't we have supported x in this check?", search for issue, see explanation.

@romani
Copy link
Member

romani commented Jun 19, 2024

Problem with this is that users (and us) won't find this in the future

I more about CI enforcement, one day in future such Input will help to catch some regression. It is all about diversity of Inputs, Check can have extra property and this diversity will be beneficial.

@nrmancuso
Copy link
Member

You are conflating two completely different concepts:

  1. Opening issues to create a link between language features and checks.
  2. Adding new test cases.

We will continue to open issues to create a link for users and us between language features and checks, and will continue to create new test cases for most checks where we don’t need an update. It’s that simple. We will not exhaustively add test cases for every single thing. ROI is low for this.

@nrmancuso
Copy link
Member

Let's add a simple test case for this one and move on.

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

4 participants