Skip to content

[java] MethodReturnsInternalArray: Various false negatives with local aliases and conditional expressions #6901

Closed

Description

@MarkLee131

Affects PMD Version: 7.26.0

Rule: MethodReturnsInternalArray

Description:

MethodReturnsInternalArray only reports return <field> syntactically. The same array returned through a local alias, a conditional expression, branch assignments, or a generic wrapper is not reported. Behaviourally identical escapes, so these are false negatives.

Code Sample demonstrating the issue:

public class ArrDirect {             // reported
    private int[] data = new int[4];

    public int[] getData() {
        return data;
    }
}
public class ArrAlias {              // NOT reported
    private int[] data = new int[4];

    public int[] getData() {
        int[] result = data;
        return result;
    }
}
public class ArrConditional {        // NOT reported
    private int[] data = new int[4];
    private boolean flag;

    public int[] getData() {
        return flag ? data : new int[0];
    }
}
public class ArrBranches {           // NOT reported
    private int[] data = new int[4];
    private boolean flag;

    public int[] getData() {
        int[] result;
        if (flag) {
            result = data;
        } else {
            result = new int[0];
        }
        return result;
    }
}
public class ArrVariant {            // NOT reported (original reproducer)
    private int[] data = new int[4];

    public int[] getData() {
        return java.util.Collections.singletonList(data).get(0);
    }
}

Expected outcome:

PMD should report a MethodReturnsInternalArray violation in each class above; in 7.26.0 only ArrDirect is reported. Returning a defensive copy such as data.clone() or Arrays.copyOf(...) remains unreported.

Running PMD through: CLI

These examples match the test cases added in #6940.

Activity

  1. added
    a:false-negative
    on Jul 22, 2026
  2. changed the title [-][java] MethodReturnsInternalArray: not reported when the same array escapes through Collections.singletonList(data).get(0)[/-] [+][java] MethodReturnsInternalArray: False negative when the same array escapes through Collections.singletonList(data).get(0)[/+] on Jul 22, 2026
  3. added 3 commits that reference this issue on Aug 12, 2026
    df9342a
    b701ef0
    174cd15
  4. fudianchn commented on Sep 22, 2026

    @fudianchn
    Contributor

    @adangel As discussed in #6940, I can't edit this issue. Proposed title and description for a maintainer to copy:

    Proposed title:

    [java] MethodReturnsInternalArray: Various false negatives with local aliases and conditional expressions

    Proposed description:

    Affects PMD Version: 7.26.0

    Rule: MethodReturnsInternalArray
    https://docs.pmd-code.org/latest/pmd_rules_java_bestpractices.html#methodreturnsinternalarray

    Description:

    MethodReturnsInternalArray only reports return <field> syntactically. The same array returned through a local alias, a conditional expression, branch assignments, or a generic wrapper is not reported. Behaviourally identical escapes, so these are false negatives.

    Code Sample demonstrating the issue:

    public class ArrDirect {             // reported
        private int[] data = new int[4];
    
        public int[] getData() {
            return data;
        }
    }
    public class ArrAlias {              // NOT reported
        private int[] data = new int[4];
    
        public int[] getData() {
            int[] result = data;
            return result;
        }
    }
    public class ArrConditional {        // NOT reported
        private int[] data = new int[4];
        private boolean flag;
    
        public int[] getData() {
            return flag ? data : new int[0];
        }
    }
    public class ArrBranches {           // NOT reported
        private int[] data = new int[4];
        private boolean flag;
    
        public int[] getData() {
            int[] result;
            if (flag) {
                result = data;
            } else {
                result = new int[0];
            }
            return result;
        }
    }
    public class ArrVariant {            // NOT reported (original reproducer)
        private int[] data = new int[4];
    
        public int[] getData() {
            return java.util.Collections.singletonList(data).get(0);
        }
    }

    Expected outcome:

    PMD should report a MethodReturnsInternalArray violation in each class above; in 7.26.0 only ArrDirect is reported. Returning a defensive copy such as data.clone() or Arrays.copyOf(...) remains unreported.

    Running PMD through: CLI

    These examples match the test cases added in #6940.

  5. changed the title [-][java] MethodReturnsInternalArray: False negative when the same array escapes through Collections.singletonList(data).get(0)[/-] [+][java] MethodReturnsInternalArray: Various false negatives with local aliases and conditional expressions[/+] on Sep 24, 2026
  6. added this to the 7.28.0 milestone on Sep 24, 2026
  7. adangel commented on Sep 24, 2026

    @adangel
    Member

    @fudianchn Thanks for the updated description!

  8. added a commit that references this issue on Sep 24, 2026
    4b3af04
  9. added
    in:data-flow
    on Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-negativein:data-flow

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions