New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequireThisCheck: false positive on static field #3335

Closed
rnveach opened this Issue Jul 1, 2016 · 4 comments

Comments

Projects
None yet
2 participants
@rnveach
Member

rnveach commented Jul 1, 2016

$ cat TestClass.java
public final class TestClass {
    public static String field1 = "";

    public static String field2 = new String(field1);

    public String get() {
        return field1;
    }
}

$ cat TestConfig.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="RequireThisCheck">
<property name="checkFields" value="true" />
<property name="checkMethods" value="false" />
<property name="validateOnlyOverlapping" value="false" />
</module>
    </module>
</module>

$ java -jar checkstyle-7.0-all.jar -c TestConfig.xml TestClass.java
Starting audit...
[ERROR] TestClass.java:4:46: Reference to instance variable 'field1' needs "this.". [RequireThis]
[ERROR] TestClass.java:7:16: Reference to instance variable 'field1' needs "this.". [RequireThis]
Audit done.
Checkstyle ends with 2 errors.

I am expecting no violations. These fields are defined as static and cannot be prefixed with this..

@romani romani added the approved label Jul 1, 2016

@rnveach

This comment has been minimized.

Show comment
Hide comment
@rnveach

rnveach Jul 2, 2016

Member

Apparently I was wrong about static fields can't be prefixed with this.. They can be and it isn't a compile error as long as the method/context isn't static.
Example 1 - Eclipse gives the warning The static field fieldStatic should be accessed in a static way
Example 2 - Eclipse gives the error Cannot use this in a static context
The correct way to prefix them would be with the class name and not this.

@romani
Line 4's violation is still incorrect. The context is a static field so we still can't use this. there.

Line 7's violation is correct, but I would think the message should say to use the class name instead of this.. Not sure if we should still include it in this check, or make a new check specifically for static instances.

Since I wasn't expecting these violations anyways, I should have used validateOnlyOverlapping as true, but that only applies to static fields. I still want to validate all non-static fields as requiring this. What are your thoughts?

Member

rnveach commented Jul 2, 2016

Apparently I was wrong about static fields can't be prefixed with this.. They can be and it isn't a compile error as long as the method/context isn't static.
Example 1 - Eclipse gives the warning The static field fieldStatic should be accessed in a static way
Example 2 - Eclipse gives the error Cannot use this in a static context
The correct way to prefix them would be with the class name and not this.

@romani
Line 4's violation is still incorrect. The context is a static field so we still can't use this. there.

Line 7's violation is correct, but I would think the message should say to use the class name instead of this.. Not sure if we should still include it in this check, or make a new check specifically for static instances.

Since I wasn't expecting these violations anyways, I should have used validateOnlyOverlapping as true, but that only applies to static fields. I still want to validate all non-static fields as requiring this. What are your thoughts?

@romani

This comment has been minimized.

Show comment
Hide comment
@romani

romani Jul 2, 2016

Member
  1. first of all please make input file compilable(javac) as we demand from other users.

  2. If file is not compilable - we do not guaranty good behaviour of Checks even they are not crashing. Any violation is OK if file is not compilable by javac.

General idea was to skip all static fields from "this." validation , so if input is compilable all violations looks like problems.

I still want to validate all non-static fields as requiring this.

What is a point of "this." before static field ?

Line 7's violation is correct, but I would think the message should say to use the class name instead of this.. Not sure if we should still include it in this check, or make a new check specifically for static instances.

yes, this is just another Check.

Member

romani commented Jul 2, 2016

  1. first of all please make input file compilable(javac) as we demand from other users.

  2. If file is not compilable - we do not guaranty good behaviour of Checks even they are not crashing. Any violation is OK if file is not compilable by javac.

General idea was to skip all static fields from "this." validation , so if input is compilable all violations looks like problems.

I still want to validate all non-static fields as requiring this.

What is a point of "this." before static field ?

Line 7's violation is correct, but I would think the message should say to use the class name instead of this.. Not sure if we should still include it in this check, or make a new check specifically for static instances.

yes, this is just another Check.

@rnveach

This comment has been minimized.

Show comment
Hide comment
@rnveach

rnveach Jul 2, 2016

Member

Input file is now compilable without extra sources.

Member

rnveach commented Jul 2, 2016

Input file is now compilable without extra sources.

@romani

This comment has been minimized.

Show comment
Hide comment
@romani

romani Jul 26, 2016

Member

fix is merged.

Member

romani commented Jul 26, 2016

fix is merged.

@romani romani closed this Jul 26, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment