You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[java] MethodReturnsInternalArray: Various false negatives with local aliases and conditional expressions #6901
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.
publicclassArrAlias { // NOT reportedprivateint[] data = newint[4];
publicint[] getData() {
int[] result = data;
returnresult;
}
}
publicclassArrConditional { // NOT reportedprivateint[] data = newint[4];
privatebooleanflag;
publicint[] getData() {
returnflag ? data : newint[0];
}
}
publicclassArrBranches { // NOT reportedprivateint[] data = newint[4];
privatebooleanflag;
publicint[] getData() {
int[] result;
if (flag) {
result = data;
} else {
result = newint[0];
}
returnresult;
}
}
publicclassArrVariant { // NOT reported (original reproducer)privateint[] data = newint[4];
publicint[] getData() {
returnjava.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.
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
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.
publicclassArrAlias { // NOT reportedprivateint[] data = newint[4];
publicint[] getData() {
int[] result = data;
returnresult;
}
}
publicclassArrConditional { // NOT reportedprivateint[] data = newint[4];
privatebooleanflag;
publicint[] getData() {
returnflag ? data : newint[0];
}
}
publicclassArrBranches { // NOT reportedprivateint[] data = newint[4];
privatebooleanflag;
publicint[] getData() {
int[] result;
if (flag) {
result = data;
} else {
result = newint[0];
}
returnresult;
}
}
publicclassArrVariant { // NOT reported (original reproducer)privateint[] data = newint[4];
publicint[] getData() {
returnjava.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.
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
Activity
[-][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)[/+][java] Fix pmd#6901: Track internal array escapes through expressions
[java] Fix pmd#6901: Track internal array escapes through expressions
[java] Fix pmd#6901: Track internal array escapes through expressions
fudianchn commentedon Sep 22, 2026
@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:
MethodReturnsInternalArrayonly reportsreturn <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:
Expected outcome:
PMD should report a
MethodReturnsInternalArrayviolation in each class above; in 7.26.0 onlyArrDirectis reported. Returning a defensive copy such asdata.clone()orArrays.copyOf(...)remains unreported.Running PMD through: CLI
These examples match the test cases added in #6940.
[-][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[/+]adangel commentedon Sep 24, 2026
@fudianchn Thanks for the updated description!
[java] Fix pmd#6901: MethodReturnsInternalArray: Track internal array…