Skip to content

Latest commit

 

History

History
29 lines (24 loc) · 687 Bytes

File metadata and controls

29 lines (24 loc) · 687 Bytes

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