Java


JAVA.STRUCT.EBS : Empty Branch Statement (Java)

Summary

The result of a test is not used.

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).

Properties

Class Name Empty Branch Statement (Java)
Significance reliability
Mnemonic JAVA.STRUCT.EBS
Categories
CWE CWE:1071 Empty Code Block
Availability Available for Java only.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Empty Branch Statement (Java)"

Example

public class Main {
  public static void main(String[] args) {
      if (args.length < 2);    /* Empty Branch Statement (Java) warning issued here
                                * - semicolon immediately after IF statement. 
                                */ 
          System.exit(-1);

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

      test(17);

      Object o = new Object();
      if (o != new Object())   /* Redundant Condition (Java) warning issued here
                                * - test always succeeds, because always comparing two distinct objects. 
                                */ 
          System.out.println("distinct");
  }

  public static void go(boolean b1, boolean b2) {
    if (b1 = true)             /* Redundant Condition (Java) warning issued here
                                * - this is an assignment to b1 (not an equality test) whose value is always true.
                                */ 
        System.out.println("yes");
  }

  private static void test(int i) {
    if (i == 17)               /* Redundant Condition (Java) warning issued here
                                * - i is always 17 at runtime.
                                */ 
      System.out.println("17");
  }
}

Resolution

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.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.