Checker UselessCall

belongs to group Basic
Identify useless method calls

Frameworks supported by this checker

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

Warnings generated by this checker

  • UselessCallForIntegralValueWarning: a call is useless when its argument is an integral value [ CWE227 ]
  • UselessCallWarning: a method call seems useless [ CWE398 ]

Options accepted by this checker

  • none

Annotations understood by this checker

  • @com.juliasoft.julia.checkers.uselessCall.UselessCall


Description

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.

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.

Consider the following program:

using System;

namespace DocumentationExamples
{

    public class UselessCall
    {
        private readonly string name;
        private readonly string surname;
        private readonly DateTime birthDay;
        public UselessCall(string name, string surname)
        {
            this.name = name;
            this.surname = surname;
            birthDay = new DateTime();
        }
        public static void Main(string[] args)
        {
            UselessCall s = new UselessCall("John", "Lennon");
            Print(s.ToString());
        }
        private static void Print(object s)
        {
            Console.WriteLine(s.ToString());
        }
        public override string ToString()
        {
            return "I'm " + name.ToString() + surname.ToString() + "." 
              + " My birthday is on " + birthDay.ToString();
        }
    }
}

This checker issues the following warnings:

DocumentationExamples.cs:24: [UselessCall: UselessCallWarning] This call to method "ToString" seems useless: can you replace it with a cast to "System.String"?
DocumentationExamples.cs:28: [UselessCall: UselessCallWarning] This call to method "ToString" seems useless: can you replace it with a cast to "System.String"?

At line 24, the parameter s has type System.Object but will always hold a System.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 20.

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