Skip to content

[java] MethodReturnsInternalArray does not consider static final fields and fields initialized with empty array #3160

@oowekyala

Description

@oowekyala

Affects PMD Version: tbd

Rule: MethodReturnsInternalArray, https://pmd.github.io/latest/pmd_rules_java_bestpractices.html#methodreturnsinternalarray

Description: Static final fields are currently ignored as per https://sourceforge.net/p/pmd/bugs/1475/. However, this is only safe because the array has length zero. Sharing non-empty arrays is problematic whether the field is final or not, so it doesn't make sense to forbid one and allow the other.

Code Sample demonstrating the issue:

public class MyClass {
    private static final String[] FOO_BAR = new String[] { "foo", "bar" };
    public String[] call() { return FOO_BAR; }
}

Second example:

public class MyClass {
    private String[] fooBar = new String[0];
    public String[] call() { return fooBar; }
}

Expected outcome: A violation should be reported on the first example

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-negativePMD doesn't flag a problematic piece of code

    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