BadInstanceof
instanceof used in a way that is equivalent to a null check.

Severity
WARNING
Tags
Simplification
Has Fix?

The problem

Flags instanceof checks where the expression can be determined to be a supertype of the type it is compared to.

JLS 15.28 specifically calls instanceof out as not being a compile-time constant expression, so the usage of this pattern can lead to unreachable code that won’t be flagged by the compiler:

class Foo {
  void doSomething() {
    if (this instanceof Foo) { // BAD: always true
      return;
    }
    interestingProcessing();
  }
}

In general, an instanceof comparison against a superclass is equivalent to a null check:

foo instanceof Foo
foo != null

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("BadInstanceof") to the enclosing element.