Checker Deadcode

belongs to group Basic
Identify methods or constructors never used

Frameworks supported by this checker

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

Warnings generated by this checker

  • ClassNeverInstantiatedWarning: a class is never instantiated [ CWE561 ]
  • UncalledWarning: a method or constructor is not called [ CWE561 ]
  • UnreachableInstructionWarning: an instruction will never be executed [ CWE561 ]

Options accepted by this checker

  • none

Annotations understood by this checker

  • none


Description

Methods or constructors never used in the code are often harmless, except for an increase in size of the application. In some cases, however, unused methods are the sign of a stale application interface that needs cleaning or of the necessity to introduce an abstract method.

Action: Verify if the application interface can be simplified or an abstract method could replace a non-abstract method.

Methods or constructors never used in the code are often harmless, except for an increase in size of the application. In some cases, however, unused methods are the sign of a stale application interface that needs cleaning or of the necessity to introduce an abstract method.

Action: Verify if the application interface can be simplified or an abstract method could replace a non-abstract method.

Examples

Consider the following classes:

public class Main {
  public static void main(String[] args) {
    A b = new B();
    A c = new C();

    test(b);
    test(c);
  }

  private static void test(A a) {
    System.out.println(a.m(13));
  }
}
public class A {
  public int m(int p) {
    return p + 1;
  }
}
public class B extends A {
  @Override
  public int m(int p) {
    return p + 2;
  }
}
public class C extends A {
  @Override
  public int m(int p) {
    return p * 2;
  }
}

This checker issues the following warning:

A.java:3: [Deadcode: UncalledWarning] Method A.m(int):int is not reachable

since the implementation of method m inside class A is never called: only its redefinitions inside classes B and C are called.

In this example, the programmer should probably introduce an abstract definition for m inside class A and make the class A itself abstract, as follows:

public abstract class A {
  public abstract int m(int p);
}

Note that no warning is issued, in this case, if the application is analyzed in library mode, since in that case Julia assumes that all non-private methods might be called from outside the application and hence the implementation of method m inside class A is considered as reachable.

Consider the following classes:

using System;

namespace DocumentationExamples
{

    public class Deadcode
    {
        public static void Main(string[] args)
        {
            DeadcodeA b = new DeadcodeB();
            DeadcodeA c = new DeadcodeC();
            Test(b);
            Test(c);
        }
        private static void Test(DeadcodeA a)
        {
            Console.WriteLine(a.M(13));
        }
    }
    public class DeadcodeA
    {
        public virtual int M(int p)
        {
            return p + 1;
        }
    }
    public class DeadcodeB : DeadcodeA
    {
        public override int M(int p)
        {
            return p + 2;
        }
    }
    public class DeadcodeC : DeadcodeA
    {
        public override int M(int p)
        {
            return p * 2;
        }
    }
}
Information: The result of the analysis may be different depending on whether the Entry points global option is selected or not as ONLY_STANDAT-ENTRIES in the analysis.

This checker issues the following warning:

DocumentationExamples.cs:23: [Deadcode: UncalledWarning] Method "M" is unreachable

since the implementation of method M inside class DeadcodeA is never called: only its redefinitions inside classes DeadcodeB and DeadcodeC are called.

In this example, the programmer should probably introduce an abstract definition for M inside class DeadcodeA and make the class DeadcodeA itself abstract, as follows:

public abstract class DeadcodeA 
{
	public abstract int M(int p);
}

Note that no warning is issued, in this case, if the application is analyzed in library mode, since in that case Julia assumes that all non-private methods might be called from outside the application and hence the implementation of method M inside class DeadcodeA is considered as reachable.