Checker UselessInstanceof

belongs to group Basic
Identify useless instanceof operations

Frameworks supported by this checker

  • java up to 11
  • android up to API level 28
  • dotnet

Warnings generated by this checker

  • ImpossibleInstanceofWarning: an instanceof test is always true or always false [ CWE570 ]
  • TautologicalInstanceofWarning: an instanceof test is always true or always false [ CWE571 ]
  • UnexpectedInstanceofWarning: an instanceof test should not be used [ CWE227 ]

Options accepted by this checker

  • none

Annotations understood by this checker

  • none


Description

This checker looks for instanceof operators that are useless, in the sense that they are always true or always false and can hence be removed from the code. This can improve the efficiency of the program. Moreover, a useless instanceof test is often the sign of a programming bug.

Action: check if the instanceof operator can be actually removed and that it is not the sign of an actual programming bug.

This checker looks for is operators that are useless, in the sense that they are always true or always false and can hence be removed from the code. This can improve the efficiency of the program. Moreover, a useless is test is often the sign of a programming bug.

Action: check if the is operator can be actually removed and that it is not the sign of an actual programming bug.

Examples

Consider the following program:

public class Main {
  public static void main(String[] args) {
    A a = new B();
    switch ((int) (System.currentTimeMillis() % 3)) {
      case 0: case 1:
        a = new A(); break;
      case 2:
        a = new C(); break;
    }
    if (a instanceof C)
      foo(a);
  }

  private static void foo(A par) {
    if (par instanceof B)   // this check is useless
      System.out.println("par is a B");
  }
}

public class A {}
public class B extends A {}
public class C extends B {}

This checker issues the following warning:

Main.java:15: [UselessInstanceof: TautologicalInstanceofWarning] This instanceof seems useless

since it is always true and can be removed. In this case, the programmer should probably consider to define method foo as foo(B par).

Consider the following program:

using System;

namespace DocumentationExamples
{
    public class UselessInstanceof
    {
        public static void Main(string[] args)
        {
            UselessInstanceofA a = new UselessInstanceofB();
            switch (DateTime.Now.Millisecond % 3)
            {
                case 0:
                case 1:
                    a = new UselessInstanceofA(); break;
                case 2:
                    a = new UselessInstanceofC(); break;
            }
            if (a is UselessInstanceofC)
                Foo(a);
        }
        private static void Foo(UselessInstanceofA par)
        {
            if (par is UselessInstanceofB)
                Console.WriteLine("par is a B");
        }
    }
    public class UselessInstanceofA { }
    public class UselessInstanceofB : UselessInstanceofA { }
    public class UselessInstanceofC : UselessInstanceofB { }
}

This checker issues the following warning:

DocumentationExamples.cs:23: [UselessInstanceof: TautologicalInstanceofWarning] This instanceof seems useless

since it is always true and can be removed. In this case, the programmer should probably consider to define method Foo as Foo(B par).