GeezerVison is showing github.com — [java] Fix #4870: ArrayIsStoredDirectly checks enum constructors by lyn1dev · Pull Request #7381 · pmd/pmd
Conversation
Describe the PR
ArrayIsStoredDirectly did not report an array that an enum constructor stores.
Enum constructors are implicitly private and cannot be declared with another
visibility, so the allowPrivate exemption (default true) disabled the rule for
every one of them. In the reported case an enum constant passes an array that is
declared outside the enum (C(strings)), so the stored array stays reachable and
mutable by the caller.
The rule now checks enum constructors. A varargs parameter of such a constructor
is only reported when an enum constant passes an array to it; when the constants
pass plain elements, the compiler creates the array for the call, so nothing
outside can reach the stored array. That keeps the behaviour agreed in #1413.
Related issues
Ready?
- Added unit tests for fixed bug/feature
- Passing all unit tests
- Complete build
./mvnw clean verify passes (checked automatically by github actions)
- Added (in-code) documentation (if needed)
Testing
Run in pmd-java with JDK 21 (Temurin 21.0.12.1):
./mvnw -pl pmd-java -Dtest=ArrayIsStoredDirectlyTest test -
25 tests pass with the change. With the rule reverted to main, the two new
cases fail with expected: <1> but was: <0>.
./mvnw -pl pmd-java verify - 10857 tests, 0 failures, 0 Checkstyle
violations; pmd:check and cpd-check also pass.
AI was used to write this code.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
<expected-linenumbers>8</expected-linenumbers>
|
|
|
<code><![CDATA[
|
|
|
public enum Foo {
|
|
|
A(new String[]{"a"}),
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The case where the array is directly initialized in the parameter is equivalent to the varargs syntax, so it would be nice to allow it too.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
lyn1dev
Nov 26, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in d5da9de to allow inline array allocations (ASTArrayAllocation) and null literals alongside varargs expansions in enum constructors, so A(new String[]{a}) is no longer flagged while passing an external array reference (such as C(strings)) is still reported. Also scoped enum constructor detection to direct ASTEnumBody children (excluding nested classes inside enums), handled this(...) delegations within the enum body, and updated the rule description and XML test cases (ArrayIsStoredDirectlyTest: 27 tests passing, checkstyle:check: 0 violations).
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Documentation Preview
Compared to main:
This changeset changes 0 violations,
introduces 11 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 16 errors and 9 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.
Regression Tester Report
(comment created at 2026-11-28 00:12:41+00:00 for 80c8b27)
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This branch has not been deployed
No deployments
Reviewers
zbynek
zbynek left review comments
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|