Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java]: UseArraysAsList should not warn when elements are skipped in array #5071

Open
tsmock opened this issue Jun 17, 2024 · 0 comments
Open
Labels
a:false-positive PMD flags a piece of code that is not problematic

Comments

@tsmock
Copy link

tsmock commented Jun 17, 2024

Affects PMD Version: 7.2.0

Rule: UseArraysAsList

Please provide the rule name and a link to the rule documentation:
https://docs.pmd-code.org/latest/pmd_rules_java_performance.html#usearraysaslist

Description: UseArraysAsList should not warn when elements are skipped in array

Code Sample demonstrating the issue:

import java.util.*;
public class Test {
  public static void main(String... args) {
    String[] entrySet = {"key1", "value1", "key2", "value2", "key3", "value3"};
    for (String key : keySet(entrySet)) {
      System.out.println(key);
    }
  }

  private static Collection<String> keySet(String... entrySet) {
    final Set<String> result = new HashSet<>(entrySet.length / 2);
    for (int i = 0; i < entrySet.length; i += 2) {
      result.add(entrySet[i]);
    }
    return result;
  }
}

Expected outcome: No warning

PMD reports a violation at line 13, but that's wrong. That's a false positive.
Full message:

[INFO] PMD version: 7.2.0
[INFO] PMD Failure: Test:13 Rule:UseArraysAsList Priority:3 Use asList instead of tight loops.

Running PMD through: Maven

Possibly related to:

@tsmock tsmock added the a:false-positive PMD flags a piece of code that is not problematic label Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

No branches or pull requests

1 participant