Menu

#1553 [java] UnusedPrivateField - false positive for private field declared and used in inner class referring to outer class

New Tickets
not-reproducible
None
PMD
3-Major
Bug
5.5.2
rulesets/java/unusedcode.xml/UnusedPrivateField
2017-03-16
2016-11-30
Felix Otto
No

False positive for private field referring to outer class

public class Foo {
}

class Bar {

    private final Foo foo; // PMD complains about unused private field

    public Bar(Foo foo) {
        this.foo = foo;
    }

    public String toString() {
        return this.foo.toString();
    }
}

No problem using another type

public class Foo {
}

class Bar {

    private final String foo; // false positive is not reported using String

    public Bar(String foo) {
        this.foo = foo;
    }

    public String toString() {
        return this.foo.toString();
    }
}

No problem with another really unused field before

public class Foo {
}

class Bar {

    // with this unused field PMD doesn't complain anymore about "foo" below
    private String var;
    private final Foo foo;

    public Bar(Foo foo) {
        this.foo = foo;
    }

    public String toString() {
        return this.foo.toString();
    }
}

Discussion

  • Andreas Dangel

    Andreas Dangel - 2017-01-08
    • summary: UnusedPrivateField - false positive for private field declared and used in inner class referring to outer class --> [java] UnusedPrivateField - false positive for private field declared and used in inner class referring to outer class
     
  • Juan Sotuyo

    Juan Sotuyo - 2017-02-22
    • status: open --> not-reproducible
    • assigned_to: Juan Sotuyo
     
  • Juan Sotuyo

    Juan Sotuyo - 2017-02-22

    Neither the False Positive of the first snippet nor the False Negative of the third one are reproducible under the current master branch.

    Possibly, the underlaying bug was in the symbol table analysis, and fixed with PRs https://github.com/pmd/pmd/pull/242 and https://github.com/pmd/pmd/pull/258

     
  • Felix Otto

    Felix Otto - 2017-03-16

    It's also fine for me. Obviously it has been fixed. Let's close this bug report.

     

Log in to post a comment.

MongoDB Logo MongoDB