netbeans icon indicating copy to clipboard operation
netbeans copied to clipboard

False positive for "Unbalanced read/write with collections" hint

Open nmatt opened this issue 3 years ago • 0 comments

Apache NetBeans version

Apache NetBeans 15

What happened

Example Java source code:

public final class Example
{
    private final List<String> items;  // (*)

    public Example(List<String> items)
    {
        // defensive copy:
        this.items = Collections.unmodifiableList(new ArrayList<>(items));
    }

    public List<String> getItems()
    {
        return items;
    }

    @Override
    public String toString()
    {
        return items.isEmpty() ? "(no items)" : String.join(", ", items);
    }
}

NetBeans shows a warning "the collection is never added to" on the items field (*), This is triggered by the isEmpty() invocation in toString(). However, NetBeans should either notice that this is a read-only collection (difficult), or else notice that the field is exposed to client code through getItems(), which allows client code to (attempt to) add to the list.

NetBeans 12.2 (which I used previously) does not show that warning, although it does support the same hint ("Unbalanced read/write with collections"). So this appears to be a regression.

How to reproduce

See example source code above.

Did this work correctly in an earlier version?

Apache NetBeans 12.3 or earlier

Operating System

Windows 10 Enterprise 22H1

JDK

Oracle JDK 8u271

Apache NetBeans packaging

Apache NetBeans provided installer

Anything else

No response

Are you willing to submit a pull request?

No

Code of Conduct

Yes

nmatt avatar Oct 12 '22 22:10 nmatt