Skip to content

UnnecessaryVarargsArrayCreation false positive when an ambiguity is possible #1525

@volodya-lombrozo

Description

@volodya-lombrozo

Consider the following expression (constructor):

new ElegantObject(
    "fibonacci",
    new String[]{
        "+package org.eolang.examples",
        "+architect yegor256@gmail.com",
        "",
        "# This is the main abstract object that",
        "# represents n-th Fibonacci number",
        "[n] > fibonacci",
        "  if. > @",
        "    lt.",
        "      n",
        
        "    n",
        "    plus.",
        "      ^.fibonacci",
        "        n.minus 1",
        "      ^.fibonacci",
        "        n.minus 2",
    }
)

qulice complaints on it with the following error:

[INFO] PMD: src/test/java/integration/JarIT.java[98-126]: Unnecessary explicit array creation for varargs method call (UnnecessaryVarargsArrayCreation)

because the ElegantObject has the following constructor:

private ElegantObject(final String file, final String... content) {
    this.file = file;
    this.content = String.join("\n", content);
}

That said, we can't just remove new String[]:

new ElegantObject(
    "fibonacci",
    "+package org.eolang.examples",
    "+architect yegor256@gmail.com",
    "",
    "# This is the main abstract object that",
    "# represents n-th Fibonacci number",
    "[n] > fibonacci",
    "  if. > @",
    "    lt.",
    "      n",
    "      2",
    "    n",
    "    plus.",
    "      ^.fibonacci",
    "        n.minus 1",
    "      ^.fibonacci",
    "        n.minus 2"
)

It's impossible, because we will get a compiler error as the following one:

The constructor JarIT.ElegantObject(String[]) is am

Expected behaviour:

qulice doesn't complain on methods where it's impossible to replace array creation with varags.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions