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 Unnamed Variables & Patterns Syntax: WhitespaceAfter #15057

Closed
mahfouz72 opened this issue Jun 20, 2024 · 3 comments · Fixed by #15447
Closed

Add Check Support for Java 21 Unnamed Variables & Patterns Syntax: WhitespaceAfter #15057

mahfouz72 opened this issue Jun 20, 2024 · 3 comments · Fixed by #15447

Comments

@mahfouz72
Copy link
Member

mahfouz72 commented Jun 20, 2024

child of #14942

I have read check documentation: https://checkstyle.sourceforge.io/checks/whitespace/whitespaceafter.html#WhitespaceAfter
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="WhitespaceAfter">
        </module>
    </module>
</module>
PS D:\CS\test> cat src/Test.java                                                
import java.util.List;

record ColoredPoint(int p, int x, int c) { }
record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) { }

public class Test {
    void test(Object obj) {

        switch (obj) {
            case ColoredPoint(int x, _, int c) when c == 0 -> { // expected violation

            }
            case ColoredPoint(int x, _ , int c) -> {}
            default -> { }
        }
    }
}

PS D:\CS\test> java  -jar checkstyle-10.17.0-all.jar -c config.xml src/Test.java
Starting audit...
Audit done.
PS D:\CS\test> 

Describe what you want in detail.

I think the check should support UNNAMED_PATTERN_DEF. IMO, it is visually cleaner when we have a space after the underscore. do you think it should be no white space as someone suggested before?

@rnveach
Copy link
Member

rnveach commented Jun 22, 2024

List<String> Xs = list.stream().map(_-> "x").toList(); // expected violation
List<String> Xss = list.stream().map(_ -> "x").toList();

I agree with this violation, but this is more because there should be whitespaces around the -> and not because of the _. I would want this regardless of the variable name, or unnamed. If we don't support this for Lambda, then I think this should be another issue.

case ColoredPoint(int x, _, int c) when c == 0 -> { // expected violation

I will say at first this seems odd to me, but without any type maybe it makes some sense? I would have to code more on this to know for sure.

do you think it should be no white space as someone suggested before?

I more leaning to this.

Parameter names or anything variable name related are not supported in this check currently. This issue seems to me like an odd leap for this check.

Do we have any best practices that say this way is better?

@nrmancuso What is your thoughts?

@nrmancuso
Copy link
Member

Let's revisit this one later on.

@nrmancuso
Copy link
Member

We should rely on the surrounding tokens for checking whitespace, conceptually this is no different than any other identifier. I am approving this for a new test case only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

3 participants