mebigfatguy / fb-contrib

a FindBugs/SpotBugs plugin for doing static code analysis for java code bases
http://fb-contrib.sf.net
GNU Lesser General Public License v2.1
157 stars 45 forks source link

False positive FCBL_FIELD_COULD_BE_LOCAL for private field of nested class #460

Open nmatt opened 10 months ago

nmatt commented 10 months ago

Accesses to private fields of a nested class from the containing class (and possibly vice-versa?) are not considered, leading to false positives. This happens with sb-contrib version 7.6.4 (current release).

Example code:

public final class Example
{
    public final Type type;
    public final String id;

    public Example(Type type, String id)
    {
        this.type = requireNonNull(type);
        this.id = requireNonNull(id);
    }

    @Override
    public String toString()
    {
        return type.displayName + " <" + id + ">";
    }

    public enum Type
    {
        FOO("Foolicious"),
        BAR("Barocious");

        private final String displayName;

        private Type(String displayName)
        {
            this.displayName = displayName;
        }
    };
}

This reports FCBL_FIELD_COULD_BE_LOCAL for Type.displayName.

Making the field package-private or public does not report the bug. Similarly, when adding a Type::toString returning displayName, the bug is not reported.

(In this example, the rationale is that Type should be public. but we don't want to directly expose its display name, which is an implementation detail for use in the containing class.)