Skip to content

[java] UnusedX rules should ignore some variable names and annotations #2838

@linusjf

Description

@linusjf

Is your feature request related to a problem? Please describe.
UnusedLocalVariable, UnusedFormalParameter flag all unused variables.

Describe the solution you'd like
Please add a property to the rule specifying a pattern such as starts with $, _ or unused for the variable name so that variables that meet the criteria are excluded. Additionally, you could support custom annotations that mark the variables as Unused. e.g., @Unused.

Describe alternatives you've considered
Example code flagged by ForLoopCanBeForEach when usual for loop used:

@SuppressWarnings("PMD.UnusedLocalVariable")
  @Override
  public Solution solve() {
    int[] dp = new int[capacity + 1];

    List<Item> itemsList = new ArrayList<>(items.length);
    int i = 0;
    for (int dpVal : dp) {
      for (Item item : items) {
        if (item.weight <= i) {
          int includedVal = dp[i - item.weight] + item.value;
          if (includedVal > dp[i]) dp[i] = includedVal;
        }
      }
      ++i;
    }
    path(items, capacity, dp, itemsList);
    itemsList = Item.pack(itemsList);
    return new Solution(itemsList, dp[capacity]);
  }

Additional context
Haskell/Python style
Error-prone

Also see #2923

Metadata

Metadata

Assignees

No one assigned

    Labels

    an:enhancementAn improvement on existing features / rules

    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