fb-contrib icon indicating copy to clipboard operation
fb-contrib copied to clipboard

SUI_CONTAINS_BEFORE_REMOVE false-positive

Open boris-petrov opened this issue 5 years ago • 4 comments

if (set.contains(item)) {
    if (something == null) {
        return null;
    }

    set.remove(item);
}

This code produces a SUI_CONTAINS_BEFORE_REMOVE warning which is wrong. If something is null, the item is not removed from the set so the warning is not correct.

boris-petrov avatar May 03 '20 08:05 boris-petrov

This is quite odd logic. If something is unrelated to the presence of the item in the collection, perhaps you could put the null check outside of the contains check? It would be more efficient, too.

ThrawnCA avatar May 04 '20 21:05 ThrawnCA

@ThrawnCA - Thanks for the answer.

something is related to the presence of the item. Otherwise I wouldn't have put it in the first if. The logic is exactly as I've put it in the code - if the set contains some item and something else is true, just return, otherwise if just the set contains the item but the other thing is false, remove the item from the set and move on.

boris-petrov avatar May 05 '20 05:05 boris-petrov

We're getting a similar SUI_CONTAINS_BEFORE_ADD false-positive.

if (!set.contains(item) && expensiveLogic(item)) {
    set.add(item)
}

!set.contains(item) is used as a quick check to avoid calling the more expensive method unnecessarily.

(Not sure if this should be its own issue, but it seems related to this one...)

snago avatar Feb 24 '21 13:02 snago

The same that snago happen with us, we're only add after a complex logic, but there's no need to do such logic if the item is already in set right?

This issue was open about 2.5 years ago, there's no fix for that yet @ThrawnCA?

NathanAlcantara avatar Dec 01 '22 13:12 NathanAlcantara