Skip to content

Add Check Support for Java 21 Record Pattern : HiddenField #14964

@mahfouz72

Description

@mahfouz72

child of #14890
no update is needed for this Issue. we need a new input file and a new UT for record pattern syntax

PS D:\CS\test> cat src/Test.java                                                 
public class InputHiddenFieldRecordPattern {
    private String s;
    private int x;
    static int z;

    record Color(String name, int value) { }
    record Point(Color c, int x, int y) { }

    public boolean doStuff(Object f) {
            return f instanceof Color(String s, int x);  // 2 violations
    }


    public void test(Object f) {
        if (f instanceof Color(String s, int x)) { // 2 violations
            System.out.println(s);
        }
    }

   public void test2(Object f) {
        if (f instanceof Point(Color(String s, int x), _, int z)) { // 3 violations
            System.out.println(s);
        }
        switch (f) {
            case Point(Color(String s, int x), _, _) : break; // 2 violations
            default:
                throw new IllegalStateException();
        }
   }
}
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="HiddenField">

    </module>
 </module>
</module>
PS D:\CS\test> java  -jar checkstyle-10.14.2-all.jar -c config.xml  src/Test.java
Starting audit...
[ERROR] D:\CS\test\src\Test.java:10:46: 's' hides a field. [HiddenField]
[ERROR] D:\CS\test\src\Test.java:10:53: 'x' hides a field. [HiddenField]
[ERROR] D:\CS\test\src\Test.java:15:39: 's' hides a field. [HiddenField]
[ERROR] D:\CS\test\src\Test.java:15:46: 'x' hides a field. [HiddenField]
[ERROR] D:\CS\test\src\Test.java:21:45: 's' hides a field. [HiddenField]
[ERROR] D:\CS\test\src\Test.java:21:52: 'x' hides a field. [HiddenField]
[ERROR] D:\CS\test\src\Test.java:21:63: 'z' hides a field. [HiddenField]
[ERROR] D:\CS\test\src\Test.java:25:37: 's' hides a field. [HiddenField]
[ERROR] D:\CS\test\src\Test.java:25:44: 'x' hides a field. [HiddenField]
Audit done.
Checkstyle ends with 9 errors.
PS D:\CS\test> 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions