Skip to content

Add Check Support for Java 21 Record Pattern : EqualsAvoidNull #14952

@mahfouz72

Description

@mahfouz72

child of #14890

this check looks good we don't need an update because each variable in the record pattern is just a PATTERN_VARAIABLE_DEF and we support this token in this check.
this issue can be just closed or send a PR to add input file for record patterns syntax

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

public class PatternVariables {
    public void TestIllegalType(Object obj) {
        if (obj instanceof ColoredPoint(Point _, String c)) {
            System.out.println(c.equals("yellow")); // violation
        }
        if (obj instanceof Rectangle(ColoredPoint(_,String x), ColoredPoint(Point(int a, int b),String y) )) {
            boolean bb = x.equals("yellow") || y.equals("blue"); // 2 violation
            boolean c = x.equals("yellow") && y.equals("blue"); // 2 violation
        }
    }
}
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="EqualsAvoidNull">
    </module>
 </module>
</module>
PS D:\CS\test> java  -jar checkstyle-10.14.2-all.jar -c config.xml  src/PatternVariables.java
Starting audit...
[ERROR] D:\CS\test\src\PatternVariables.java:8:40: String literal expressions should be on the left side of an equals comparison. [EqualsAvoidNull]
[ERROR] D:\CS\test\src\PatternVariables.java:11:34: String literal expressions should be on the left side of an equals comparison. [EqualsAvoidNull]
[ERROR] D:\CS\test\src\PatternVariables.java:11:56: String literal expressions should be on the left side of an equals comparison. [EqualsAvoidNull]
[ERROR] D:\CS\test\src\PatternVariables.java:12:33: String literal expressions should be on the left side of an equals comparison. [EqualsAvoidNull]
[ERROR] D:\CS\test\src\PatternVariables.java:12:55: String literal expressions should be on the left side of an equals comparison. [EqualsAvoidNull]
Audit done.
Checkstyle ends with 5 errors.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions