Java

Java

Made by DeepSource

Redundant type check JAVA-W1021

Anti-pattern
Major

This code attempts to perform an instanceof check of a type with its supertype. This is a redundant operation as such a check will always return true.

For two classes A and B, where A extends B, an instance of A can also be treated as an instance of B. Thus, a check such as <some instance of A> isntanceof B will always return true.

This may have been a typo. Check if the type you are checking for is correct, and rectify it if not.

Bad Practice

Integer a = 3;

// `a` is already a Number
if (a instanceof Number) {
    // ...
}

Recommended

If the check serves no purpose, remove it. Else, verify that the proper type is being checked for.

References

  • Oracle Java Language Specification - Section 15.20.2 - Type Comparison Operator instanceof