A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/3965 below:

[java] UseArraysAsList false positive with non-trivial loops · Issue #3965 · pmd/pmd · GitHub

Affects PMD Version: 6.45.0

Rule: UseArraysAsList

Description: The rule should only report simple, trivial loops, that do nothing more than converting an array into a list.

Code Sample demonstrating the issue:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {
    public void foo(Integer[] ints) {
        List<Integer> l = new ArrayList<>(100);
        for (int i = 0; i < 100; i++) {
            switch (lookup(ints[i])) {
            case 1: l.add(ints[i]); break; // line 10 - false positive
            case 2: l.addAll(getInts(i)); break;
            }
        }

        List<Integer> anotherList = new ArrayList<>();
        for (int i : ints) {
            switch (lookup(i)) {
            case 1: anotherList.add(i); break; // line 18 - false positive
            case 2: anotherList.addAll(getInts(i)); break;
            }
        }
    }

    int lookup(int a) {
        return a;
    }

    List<Integer> getInts(int a) {
        return Arrays.asList(a);
    }
}

Expected outcome:

PMD reports a violation at lines 10 and 18, but that's wrong. That's a false positive.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4