Skip to content

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

Closed

Description

@harbulot

Affects PMD Version: 7.0.0-rc2

Rule: UseShortArrayInitializer

Code Sample demonstrating the issue:

Set<Integer> set1 = new HashSet<>();

@SuppressWarnings("unchecked")		
Set<Integer>[] setArray = new Set[] { set1 };

Expected outcome:

The code above compiles correctly, but PMD reports a UseShortArrayInitializer rule violation on the last statement.

This effectively suggests to use this instead:

Set<Integer>[] setArray = { set1 };

Unfortunately, this results in a "Compilation failure" with "generic array creation".

Using the longer form with new Set[] { ... } appears to be necessary when it's an array of parametrized type.

Running PMD through: Maven (using maven-pmd-plugin version 3.20.1-pmd-7-SNAPSHOT configured with PMD 7.0.0-rc2)

Activity

  1. added
    a:false-positive
    on May 26, 2023
  2. changed the title [-]UseShortArrayInitializer with arrays of parametrized types (generic array creation)[/-] [+][java] UseShortArrayInitializer with arrays of parametrized types (generic array creation)[/+] on May 26, 2023
  3. added
    needs:pmd7-revalidation
    and removed
    needs:pmd7-revalidation
    on Mar 17, 2024
  4. changed the title [-][java] UseShortArrayInitializer with arrays of parametrized types (generic array creation)[/-] [+][java] UseShortArrayInitializer: False positive with arrays of parametrized types (generic array creation)[/+] on Dec 22, 2025
  5. added a commit that references this issue on Oct 21, 2026
    e864eca
  6. added this to the 7.29.0 milestone on Oct 29, 2026
  7. added a commit that references this issue on Oct 29, 2026
    570fab2
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-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions