A RetroSearch Logo

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

Search Query:

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

[java] UseArraysAsList false positive with Sets · Issue #5711 · pmd/pmd · GitHub

Affects PMD Version: 7.13.0

Rule: UseArraysAsList

Description:

When adding the values to a HashSet, the rule should not report a violation. Sets don't allow duplicate values like List (so the result might be different). And also, you can't assign a Set to a List.

Code Sample demonstrating the issue:

import java.util.HashSet;
import java.util.Set;

public class Foo {
    private final Set<String> hashSet = new HashSet<>();

    public void parseData(final String dataString) {
        final String[] myArray = dataString.split(",");

        for (final String element : myArray) {
            this.hashSet.add(element); // line 11 - false positive
        }
        // e.g. this.hashSet = Arrays.asList(myArray); won't compile
    }
}

Note: Collections.addAll(this.hashSet, myArray); would work. See also #3192.

Expected outcome:

PMD reports a violation at line 11, but that's wrong. That's a false positive.

Running PMD through: Maven


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