Skip to content

[java] UnnecessaryVarargsArrayCreation: False positive when removing array creates overload ambiguity #6611

Closed

Description

@yegor256

Affects PMD Version

7.23.0

Rule

category/java/bestpractices.xml/UnnecessaryVarargsArrayCreation

Description

UnnecessaryVarargsArrayCreation flags new String[]{...} passed to a varargs parameter as unnecessary, but in some cases the explicit array is required to disambiguate between overloaded methods/constructors. Removing it produces a compile error.

Minimal reproducer

public final class Demo {
    public Demo(final String file, final String... lines) {}
    public Demo(final String... args) {}

    public static void main(final String[] a) {
        new Demo("fibonacci", new String[]{"a", "b"}); // PMD flags this
    }
}

PMD reports:

Unnecessary explicit array creation for varargs method call (UnnecessaryVarargsArrayCreation)

But applying the suggested fix (removing new String[]) does not compile:

new Demo("fibonacci", "a", "b");
Demo.java:6: error: reference to Demo is ambiguous
        new Demo("fibonacci", "a", "b");
        ^
  both constructor Demo(String,String...) in Demo
  and constructor Demo(String...) in Demo match

Expected behavior

The rule should not report a violation when the target type has another overload whose parameter types would also be applicable after the array-creation is expanded into varargs, because in that case the explicit array is load-bearing for overload resolution.

Actual behavior

The rule reports the array creation as unnecessary without checking for overload ambiguity, leading users to a non-compiling fix.

Context

Reported downstream in qulice: yegor256/qulice#1525

Activity

  1. changed the title [-]UnnecessaryVarargsArrayCreation false positive when removing array creates overload ambiguity[/-] [+][java] UnnecessaryVarargsArrayCreation false positive when removing array creates overload ambiguity[/+] on May 16, 2026
  2. added
    a:false-positive
    on May 16, 2026
  3. added this to the 7.27.0 milestone on Aug 14, 2026
  4. added 3 commits that reference this issue on Aug 14, 2026
    10f1e4b
    f66d5dc
    f722cfb
  5. changed the title [-][java] UnnecessaryVarargsArrayCreation false positive when removing array creates overload ambiguity[/-] [+][java] UnnecessaryVarargsArrayCreation: false positive when removing array creates overload ambiguity[/+] on Aug 15, 2026
  6. changed the title [-][java] UnnecessaryVarargsArrayCreation: false positive when removing array creates overload ambiguity[/-] [+][java] UnnecessaryVarargsArrayCreation: False positive when removing array creates overload ambiguity[/+] on Aug 20, 2026
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