Skip to content

Add Check Support for Java 21 Unnamed Variables & Patterns Syntax: IllegalType #15067

@mahfouz72

Description

@mahfouz72

child of #14942 :

check doc : https://checkstyle.org/checks/coding/illegaltype.html


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="IllegalType">
        </module>
    </module>
</module>
PS D:\CS\test> cat src/Test.java                                                
import java.util.LinkedHashMap;
import java.util.List;

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

public class Test {
    void test(Object obj) {
        LinkedHashMap _ = new LinkedHashMap<>();     // violation
        if (obj instanceof LinkedHashMap<?,?> _) { }          // violation, is this a false positive?
        if (obj instanceof LinkedHashMap<?,?>) { }     
    }
}

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:9:9: Usage of type 'LinkedHashMap' is not allowed. [IllegalType]
[ERROR] D:\CS\test\src\Test.java:10:28: Usage of type 'LinkedHashMap' is not allowed. [IllegalType]
Audit done.
Checkstyle ends with 2 errors.
PS D:\CS\test> 

Checks that particular classes or interfaces are never used.

I expect no violation on IllegalType that can't be used so I consider the violation on unnamed pattern variable as a false positive.
since if (obj instanceof LinkedHashMap<?,?>) { } then if (obj instanceof LinkedHashMap<?,?> _) { } should be ok. In both cases we can't use the illegal type. Therefore, it doesn't violate the rationale of the check, which is to help reduce coupling on concrete classes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions