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

belongs to group Basic

Identify useless tests


This checker looks for tests that are useless since, for instance, their outcome drives control to the same execution path, regardless of the test itself. This happens for instance when the empty statement is used after a conditional because the programmer put a semicolon after the conditional. This checker also looks for tests that always have the same outcome (always true or always false).

Action: remove the check or complete a conditional with actual then or else branch, instead of an empty statement. Verify if the useless test is the sign of a more significant algorithmic problem.

Examples


Consider the following program:

public class Main {
  public static void main(String[] args) {
    if (args.length < 2);
      System.exit(-1);

    go(System.currentTimeMillis() % 4 == 0,
       System.currentTimeMillis() % 3 == 0);

    test(17);

    Object o = new Object();
    if (o != new Object())
      System.out.println("distinct");
  }

  public static void go(boolean b1, boolean b2) {
    if (b1 = true)
      System.out.println("yes");
  }

  private static void test(int i) {
    if (i == 17)
      System.out.println("17");
  }
}

This checker issues the following warnings:

Main.java:3: [UselessTest: UselessTestWarning] The result of this test is not used and the test is hence useless
Main.java:12: [UselessTest: TestIsPredeterminedWarning] The result of this test is fixed: you are comparing a new java.lang.Object created at line 11 against a new java.lang.Object created at line 12
Main.java:17: [UselessTest: TestIsPredeterminedWarning] The result of this test is fixed: you are comparing 1 against 0
Main.java:22: [UselessTest: TestIsPredeterminedWarning] The result of this test is fixed: you are comparing 17 against 17

since there is a spurious semicolon at the end of the conditional at line 3, so that the outcome of that test does not affect the execution paths of the program. Moreover, the condition at line 12 happens between two distinct objects and can only fail. The test at line 17 is always true, since the constant true is assigned to the Boolean variable b1: there is a single equality sign instead of ==. The test at line 22 always succeeds, since only and always 17 is passed to method test().