Skip to content

RequireThisCheck: pattern variables produce false positives #10969

@rnveach

Description

@rnveach

Identified at #7504 (comment) ,

$ cat TestClass.java
public class Test {
    String p, s, n;

    public static void main(String... arguments) {
        new Test().run(arguments);
    }

    public void run(String... arguments) {
        this.p = null;
        this.s = null;
        this.n = null;

        Object o = 45;
        if (!(o instanceof String p && p.equals("sd")) || !p.equals("wq")) { // only last p is instance variable
            p = "a"; // instance var p
        } else if (!(o instanceof Integer s && o instanceof String n)) {
            p = "b"; // local var p
            s = "b"; // instance var s
            n = "b"; // instance var n
        } else {
            p = "c"; // local var p
            s = 41; // local var s
            n = "c"; // local var n
        }

        System.out.println(this.p + ":" + this.s + ":" + this.n);
    }
}

$ cat TestConfig.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">
    <property name="charset" value="UTF-8"/>

    <module name="TreeWalker">
<module name="RequireThis">
  <property name="validateOnlyOverlapping" value="false"/>
</module>
    </module>
</module>

$ java -jar checkstyle-9.0.1-all.jar -c TestConfig.xml TestClass.java
Starting audit...
[ERROR] TestClass.java:14:35: Reference to instance variable 'p' needs "this.". [RequireThis]
[ERROR] TestClass.java:14:40: Reference to instance variable 'p' needs "this.". [RequireThis]
[ERROR] TestClass.java:14:60: Reference to instance variable 'p' needs "this.". [RequireThis]
[ERROR] TestClass.java:15:13: Reference to instance variable 'p' needs "this.". [RequireThis]
[ERROR] TestClass.java:16:43: Reference to instance variable 's' needs "this.". [RequireThis]
[ERROR] TestClass.java:16:68: Reference to instance variable 'n' needs "this.". [RequireThis]
[ERROR] TestClass.java:17:13: Reference to instance variable 'p' needs "this.". [RequireThis]
[ERROR] TestClass.java:18:13: Reference to instance variable 's' needs "this.". [RequireThis]
[ERROR] TestClass.java:19:13: Reference to instance variable 'n' needs "this.". [RequireThis]
[ERROR] TestClass.java:21:13: Reference to instance variable 'p' needs "this.". [RequireThis]
[ERROR] TestClass.java:22:13: Reference to instance variable 's' needs "this.". [RequireThis]
[ERROR] TestClass.java:23:13: Reference to instance variable 'n' needs "this.". [RequireThis]
Audit done.
Checkstyle ends with 12 errors.

As the comments demonstrate, only really lines 14:60, 16, 19, and 20 should have violations as they are accessing the instance variables.

You can confirm this by running the code, which as is outputs a:null:null.

Thanks @nmancus1 for explaining all this to me.

Metadata

Metadata

Assignees

No one assigned

    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