Skip to content

[java] UseArraysAsList with condition in loop #4577

@harbulot

Description

@harbulot

Affects PMD Version: 7.0.0-rc2

Rule: UseArraysAsList

Description:

UseArraysAsList is reported even if the array can't be turned directly into a list due to other actions or conditions in the loop.

Code Sample demonstrating the issue:

    public List<String> convertToNonEmptyList(String[] arr) {
        List<String> resultList = new ArrayList<>();
        for (String item: arr) {
            if (item.isEmpty()) {
                continue;
            }
            resultList.add(item);
        }
        return resultList;
    }

Expected outcome:

In the above example, empty strings in the initial array wouldn't be present in the resulting list. This isn't equivalent to Arrays.asList(...), so UseArraysAsList shouldn't be reported.

Running PMD through: Maven (using maven-pmd-plugin version 3.20.1-pmd-7-SNAPSHOT configured with PMD 7.0.0-rc2)

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions