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: InnerAssignment #15037

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

Comments

@mahfouz72
Copy link
Member

mahfouz72 commented Jun 19, 2024

child of #14961

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


➜  src 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="InnerAssignment">

        </module>
    </module>
</module>
➜  src cat Test.java                                
record ColoredPoint(int p, int x, String c) { }
record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) { }

public class Test {

    int test(Object obj) {
        int n;
        switch (obj) {
            case ColoredPoint(int x, int y, String z) when (x >= 2)
                    -> y = 6;
            default -> {
                n = 9;
            }
        };
        return switch (obj) {
            case ColoredPoint(int x, int y, String z) when (x >= 2)
                    -> y = 6;    // violation
            default -> {
                yield n = 9;     // violation
            }
        };
    }
}
➜  src javac --enable-preview --release 21 Test.java                        
➜  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:17:26: Inner assignments should be avoided. [InnerAssignment]
[ERROR] /home/nick/IdeaProjects/tester/src/Test.java:19:25: Inner assignments should be avoided. [InnerAssignment]
Audit done.
Checkstyle ends with 2 errors.



Describe what you want in detail

we are ok here. This was an issue that I fixed recently with this PR. So this can be closed

@nrmancuso
Copy link
Member

nrmancuso commented Jun 20, 2024

Let's add a test input for this one (I don't see any record patterns or when expressions in the linked PR), @mahfouz72 can we make assignments in a when expressions?

@mahfouz72
Copy link
Member Author

Nope

PS D:\CS\test> cat src/Test.java                                                
record ColoredPoint(boolean p, int x, int c) { }
record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) { }

public class Test {
    void test(Object obj) {
       boolean n = false;
        switch (obj) {
            case ColoredPoint(boolean x, int y, int c) when (n = true) -> {
                if (n = true) { }
            }
            case ColoredPoint(boolean x, int y, int c) when (x = true) -> {
                 if (x = true) { }
            }
            default -> { }
        }
    }
}
PS D:\CS\test> javac src/Test.java                                              
src\Test.java:8: error: cannot assign to n, as it was not declared inside the guard
            case ColoredPoint(boolean x, int y, int c) when (n = true) -> {
                                                             ^
src\Test.java:11: error: cannot assign to x, as it was not declared inside the guard
            case ColoredPoint(boolean x, int y, int c) when (x = true) -> {
                                                             ^
2 errors
PS D:\CS\test> 

@rnveach
Copy link
Member

rnveach commented Jun 21, 2024

it was not declared inside the guard

Can you actually declare a variable inside the guard?

@rnveach
Copy link
Member

rnveach commented Jun 21, 2024

@mahfouz72 Can you add an example of a switch expression?
Ex: int variable = switch (....

Edit: nevermind, this is probably the same as return switch .

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

Updated the example now that we have added the EXPR token, we are good here.

@nrmancuso nrmancuso removed their assignment Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants