Skip to content

[java] Fix #4576: UseShortArrayInitializer false positive with arrays of parameterized types - #7203

Merged
adangel merged 3 commits into
pmd:mainfrom
markjoin123:cursor/fix-4576-short-array-initializer-generic-7c90
Oct 29, 2026
Merged

adangel merged 3 commits into
pmd:mainfrom
markjoin123:cursor/fix-4576-short-array-initializer-generic-7c90

Conversation

@markjoin123

Copy link
Copy Markdown
Contributor

Describe the PR

UseShortArrayInitializer suggested replacing new Set[]{...} with a short array initializer for declarations like Set<Integer>[] a = new Set[]{...}. The short form is a generic array creation error there and does not compile.

Following the guidance from the discussion in #6310, the rule now skips a declaration when some type argument of the array component type is not an unbounded wildcard. Set<?>[] and Map<?, ?>[] are still reported, since the short form is legal for them.

Added test cases for Set<Integer>[], Map<?, String>[], Map<?, ? extends String>[] (not reported) and Set<?>[], Map<?, ?>[] (still reported).

Known limitation: type arguments on a qualifying type (for example Outer<String>.Inner[]) are not considered; that case is not part of the issue.

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)

…ric arrays

The short initializer is a generic array creation error when a component
type argument is not an unbounded wildcard. Skip those declarations.
List<?>[] and Map<?, ?>[] are still reported.

Fixes pmd#4576

Co-authored-by: markxu <[email protected]>
…cation

C-style declarations such as Set<Integer> a[] = new Set[] { ... } put the
component type on the statement, so the generic-array skip missed them.
Report ArrayAllocation instead of the whole VariableDeclarator.

Fixes pmd#4576

Co-authored-by: markxu <[email protected]>
@pmd-actions-helper

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 614 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 1 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-10-21 01:53:47+00:00 for e864eca)

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@adangel adangel added this to the 7.29.0 milestone Oct 29, 2026
@adangel
adangel merged commit 570fab2 into pmd:main Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] UseShortArrayInitializer: False positive with arrays of parametrized types (generic array creation)

3 participants