False positive with Guava Cache and getIfNotPresent

Sonarcloud reports a false positive when using getIfNotPresent from the Guava Cache class:

com.google.common.cache.Cache<String, Boolean> cache;
var allowed = cache.getIfPresent(key);
if (allowed == Boolean.TRUE) {
} else if (allowed == Boolean.FALSE) {

The second condition will raise Remove this expression which always evaluates to “true” which is wrong. allowed can be null because getIfPresent() will return null if the key does not exist.

Hi Thorsten, apologies for the late response.

This is indeed a false positive, our engine is not currently aware of the behavior of that specific cache method, and fails to take into consideration the possibility of allowed being null.

I created this ticket to fix it, thank you very much for the report!

1 Like