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

FinalLocalVariable: false-negative with anonymous class #4727

Closed
romani opened this Issue Jul 15, 2017 · 2 comments

Comments

Projects
None yet
2 participants
@romani
Member

romani commented Jul 15, 2017

found at #4719 (review)

$ 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">
     <module name="TreeWalker">
        <module name="FinalLocalVariable"/>
    </module>
</module>

$ cat Test.java 
public class Test {
    public void test() {
        Object testSupport = new Object() {
            @Override
            public String toString() {
                final String dc = new String(); // comment this line to make violation happen
                return dc;
            }
        };
        testSupport.toString();
     }
}

$ java -jar checkstyle-8.0-all.jar -c config.xml Test.java 
Starting audit...
Audit done.

Expected:

[ERROR] Test.java:3:16: Variable 'testSupport' should be declared final. [FinalLocalVariable]
@rnveach

This comment has been minimized.

Show comment
Hide comment
@rnveach

rnveach Jul 16, 2017

Member

Issue is

.
branchContains is checking all children instead of the modifiers of the current variable.
It thinks code is written like final Object testSupport instead of seeing that the final is inside the inner anonymous class.

branchContains is a dangerous method to use all the time since there is no restriction on how deep it can search.

Member

rnveach commented Jul 16, 2017

Issue is

.
branchContains is checking all children instead of the modifiers of the current variable.
It thinks code is written like final Object testSupport instead of seeing that the final is inside the inner anonymous class.

branchContains is a dangerous method to use all the time since there is no restriction on how deep it can search.

@romani

This comment has been minimized.

Show comment
Hide comment
@romani

romani Jul 30, 2017

Member

fix is merged.

Member

romani commented Jul 30, 2017

fix is merged.

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