A RetroSearch Logo

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

Search Query:

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

[java] UseCollectionIsEmpty should infer local variable type from method invocation · Issue #3858 · pmd/pmd · GitHub

Affects PMD Version: 6.43.0

Rule: UseCollectionIsEmpty
https://pmd.github.io/latest/pmd_rules_java_bestpractices.html#usecollectionisempty

Description:
Please have a look at the following example, variable list is a local variable declared with type var, assigned by a method invocation getList(). The 'real' type of list should be inffered as java.util.List<String>, an implementation of interface java.util.Collection. Thus it's a good manner to use !list.isEmpty() instead of list.size() > 0 at line 8 (at point 1).

Code Sample demonstrating the issue:

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

public class Example {

    public void func() { 
        var list = getList(); 
        if (list.size()>0) { // point 1:  false negative
            System.out.println("!list.isEmpty() is better!");
            // ...
        }
    }

    private List<String> getList() { // mock
        return new ArrayList<>(); 
    }
}

Expected outcome:

PMD should report a violation at line 8 (at point 1), but doesn't. This is a false-negative.

Running PMD through: [CLI]


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