Skip to content

[java] UnusedFormalParameter and UnusedAssignment can ignore main method  #2923

@linusjf

Description

@linusjf

Affects PMD Version:

6.29.0

Description:

Rules UnusedAssignment and UnusedFormalParameter flag the args parameter in the main method when it is unreferenced.
It should be ignorable via a parameter.

@oowekyala:

With UnusedAssignment you can also rename your parameter to ignored. We can make #2838 apply to UnusedFormalParameter too. Then there would be a simple way to ignore the warning, and the fact that the parameter is ignored on purpose is explicit (this is the same kind of issue as #2893).

@Fernal73:

That's fine by me. I'd like to be able to retain the semantics of the parameter by prefixing the parameter name with underscore or argsIgnored or argsUnused.

Code Sample demonstrating the issue:

package jls;

import java.util.Objects;

public final class UnsafeVarargs<T> {

  private UnsafeVarargs() {
    throw new IllegalStateException("Private constructor.");
  }

  public static <T> T[] unsafe(T... elements) {
    // unsafe! don't ever return a parameterized varargs array
    return elements;
  }

  public static <T> T[] broken(T seed) {
    // broken! This will be an Object[] no matter what T is
    T[] plant = unsafe(seed, seed, seed);
    return plant;
  }

  public static String[] plant() {
    // ClassCastException
    String[] plants = broken("seed");
    return plants;
  }

  @SuppressWarnings("PMD.SystemPrintln")
  public static void main(String... args) {
    System.out.println(Objects.toString(plant()));
  }
}

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions