squid:S4201 leads to false positives in case of "!instance of" checks

if we check that a value is not instance of a specific type, then it matters also if the value is null or not.

  • versions used 7.9.1 (build 27448)

if (theValue == null || !(theValue instanceof List<?>)) {

It is not a False Positive. Why? Let’s analyze how it works.

Second condition:

theValue theValue instanceof List<?> !(theValue instanceof List<?>)
null false true
instance of List true false
not instance of List false true

Both conditions:

theValue theValue == null !(theValue instanceof List<?>) full condition
null true true true
instance of List false false false
not instance of List false true true

As you see, if you replace:

if (theValue == null || !(theValue instanceof List<?>)) {

by:

if (!(theValue instanceof List<?>)) {

the output will be the same:

theValue condition
null true
instance of List false
not instance of List true

RSPEC-4201

1 Like