Checker Deadcode   as of Julia version 2.5.0 (built on 4 Jul 2018)

belongs to group Basic

Identify methods or constructors never used


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.