GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] MethodReturnsInternalArray: Various false negatives with local aliases and conditional expressions · Issue #6901 · pmd/pmd

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

New issue
Copy link
New issue
Copy link
Closed
#6940
Closed
[java] MethodReturnsInternalArray: Various false negatives with local aliases and conditional expressions#6901
#6940
Copy link
Labels
a:false-negativePMD doesn't flag a problematic piece of codein:data-flowAffects the data flow analysis code
Milestone
7.28.0

Description

@MarkLee131
MarkLee131
opened on Jul 22, 2026
Issue body actions

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. MarkLee131
    added
    a:false-negativePMD doesn't flag a problematic piece of code
    on Jul 22, 2026
  2. adangel
    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. fudianchn
    added 3 commits that reference this issue on Aug 12, 2026

    [java] Fix pmd#6901: Track internal array escapes through expressions

    This issue will close once commit df9342a is merged into the 'main' branch.
    df9342a

    [java] Fix pmd#6901: Track internal array escapes through expressions

    This issue will close once commit b701ef0 is merged into the 'main' branch.
    b701ef0

    [java] Fix pmd#6901: Track internal array escapes through expressions

    This issue will close once commit 174cd15 is merged into the 'main' branch.
    174cd15
  4. fudianchn commented on Sep 22, 2026

    @fudianchn
    fudianchn
    on Sep 22, 2026
    Contributor
    More actions

    @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.

    Reacted by Andreas Dangel
  5. adangel
    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. adangel
    added this to the 7.28.0 milestone on Sep 24, 2026
  7. adangel commented on Sep 24, 2026

    @adangel
    adangel
    on Sep 24, 2026
    Member
    More actions

    @fudianchn Thanks for the updated description!

  8. adangel
    closed this as completedin #6940on Sep 24, 2026
  9. adangel
    added a commit that references this issue on Sep 24, 2026

    [java] Fix pmd#6901: MethodReturnsInternalArray: Track internal array…

    This issue will close once commit 4b3af04 is merged into the 'main' branch.
    4b3af04
  10. adangel
    added
    in:data-flowAffects the data flow analysis code
    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-negativePMD doesn't flag a problematic piece of codein:data-flowAffects the data flow analysis code

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.28.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.