#10969·checkstyle

RequireThisCheck: pattern variables produce false positives

Author: rnveachCreated Nov 21, 2021Updated Sep 28, 2026
Labelsbugapproved

Identified at https://github.com/checkstyle/checkstyle/issues/7504#issuecomment-974776332 ,

$ 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;
       // 3 violations 2 lines below, 1st 2nd are not expected
        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.