Skip to content

[java] OptimizableToArrayCall: False positive when new T[0x0] is used instead of new T[0] #6740

Closed

Description

@Chordrain

Affects PMD Version: 7.24.0

Rule: OptimizableToArrayCall

Description:

PMD's OptimizableToArrayCall rule recommends calling collection.toArray(new T[0]) over a pre-sized array such as collection.toArray(new T[collection.size()]). The zero-length form is explicitly whitelisted as compliant.

The rule decides whether the supplied array is "zero-sized" by matching the lexical image of the size literal against the string "0", rather than its numeric value. As a result, a hexadecimal zero literal 0x0 — which is numerically and at runtime identical to 0 — is not recognized as a zero-sized array, and the rule raises a false positive on collection.toArray(new T[0x0]).

Reproducer:

before.java — decimal new String[0] (no finding)

import java.util.List;

public class ToArrayCase {
    public String[] convert(List<String> list) {
        return list.toArray(new String[0]);      // compliant, not flagged
    }
}

after.java — hex new String[0x0] (false positive)

import java.util.List;

public class ToArrayCase {
    public String[] convert(List<String> list) {
        return list.toArray(new String[0x0]);    // FALSE POSITIVE: OptimizableToArrayCall
    }
}

new String[0] and new String[0x0] denote the same int constant 0 and allocate the same zero-length array. The two snippets are runtime-equivalent.

Expected behavior:

OptimizableToArrayCall should treat new T[0x0] exactly like new T[0] and produce no finding, because both express a zero-sized array. The whitelist should compare the literal's numeric value, not its lexical spelling.

Actual behavior:

  • new String[0] → 0 findings (correct).
  • new String[0x0] → 1 finding: This call to Collection.toArray() may be optimizable on the toArray call, even though the array is already zero-sized.

The rule's verdict is sensitive to the literal's textual form (0 vs 0x0) rather than its value. Other equivalent spellings such as 00, 0L cast, or 0_0 are likely affected for the same reason.

Running PMD through: CLI

Activity

  1. added
    a:false-positive
    on May 31, 2026
  2. changed the title [-][java] FP: OptimizableToArrayCall flags `new T[0x0]` as optimizable even though `0x0` is a zero-sized array[/-] [+][java] OptimizableToArrayCall: False positive when `new T[0x0]` is used instead of `new T[0]`[/+] on Jun 4, 2026
  3. added this to the 7.26.0 milestone on Jun 29, 2026
  4. added a commit that references this issue on Jun 29, 2026
    dbdec25
  5. added a commit that references this issue on Jun 29, 2026
    b3b662a
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