Skip to content

RequireThisCheck giving multiple violation for classes nested in lambdas. #11822

@Vyom-Yadav

Description

@Vyom-Yadav

I have read check documentation: https://checkstyle.sourceforge.io/config_coding.html#RequireThis
I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words

/var/tmp $ javac Test.java
/var/tmp $ cat config.xml
<!DOCTYPE module PUBLIC
        "-//Puppy Crawl//DTD Check Configuration 1.2//EN"
        "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">

<module name="Checker">
  <module name="TreeWalker">
    <module name="RequireThisCheck">
      <property name="validateOnlyOverlapping" value="false" />
    </module>
  </module>
</module>
/var/tmp $ cat Test.java
import java.util.function.Consumer;

public interface Test {

    private static void method(Consumer<String> consumer) {
        consumer.accept("foo");
    }

    private void testCtorNestedInLambda() {
        method(s -> {
            class InnerSubscriber implements Test {
                int index;

                public InnerSubscriber(int index) {
                    index = index; // 2 violations, expected 1
                }
            }
        });
    }
}
/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java $RUN_LOCALE -jar checkstyle-10.3.1-all.jar -c config.xml Test.java
Starting audit...
[ERROR] /var/tmp/Test.java:15:21: Reference to instance variable 'index' needs "this.". [RequireThis]
[ERROR] /var/tmp/Test.java:15:29: Reference to instance variable 'index' needs "this.". [RequireThis]
Audit done.
Checkstyle ends with 2 errors.

It is giving violation for both sides, the check is trying to imply this.index = this.index which isn't correct.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions