Skip to content

Add Check Support for Java 21 Unnamed Variables & Patterns Syntax: AnnotationOnSameLineCheck #15061

@mahfouz72

Description

@mahfouz72

child of #14942
Check documentation: https://checkstyle.org/checks/annotation/annotationonsameline.html#AnnotationOnSameLine

PS D:\CS\test> javac src/Test.java
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="AnnotationOnSameLine">
        </module>
    </module>
</module>
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) {
        @Deprecated
        int _ = 0;

        if (obj instanceof ColoredPoint(@Deprecated // expected violation
                                          int _, _,_)){
        }
    }
}
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:9: Annotation 'Deprecated' should be on the same line with its target. [AnnotationOnSameLine]
Audit done.
Checkstyle ends with 1 errors.
PS D:\CS\test> 


This is not very specific to unnamed patterns. but for patterns in general.
I expect a violation on the annotations on the pattern variable. The annotation should be on the same line as the pattern. We should add PATTERN_VARIABLE_DEF in the tokens

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions