Checker UselessCall   as of Julia version 2.4 (built on 23 Oct 2017)

belongs to group Basic

Identify useless method calls


This checker finds calls that are useless and can consequently be removed from the code. This means that the code becomes simpler and more efficient, but might also be the sign of a programming bug.

Action: Check if the call is useless and delete it.

Examples


Consider the following program:

import java.util.Date;

public class Student {
  private final String name;
  private final String surname;
  private final Date birthDay;

  public Student(String name, String surname) {
    this.name = name;
    this.surname = surname;
    this.birthDay = new Date();
  }

  public static void main(String[] args) {
    Student s = new Student("John", "Lennon");
    print(s.toString());
  }

  private static void print(Object s) {
    System.out.println(s.toString());
  }

  public String toString() {
    return "I'm " + name.toString() + surname.toString() + "."
      + " My birthday is on " + birthDay.toString();
  }
}

This checker issues the following warnings:

Student.java:20: [UselessCall: UselessCallWarning] This call to toString seems useless: can you replace it with a cast to java.lang.String?
Student.java:24: [UselessCall: UselessCallWarning] Useless call to toString

At line 20, the parameter s has type java.lang.Object but will always hold a java.lang.String at runtime. Hence, the call to toString is the identity function and can be removed if a cast is used for the parameter s. Here, however, it is more likely that the programmer misunderstood the semantics of the print method (transforming its parameter into a string that is then print to the screen). This is why he translates the argument into a string before invoking print. Hence, a better solution would be to remove the call to toString from line 16.

At line 24, both name and surname are strings and both calls to toString are consequently useless and can be removed.