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

belongs to group Basic

Identify where a logical bitwise operation is used instead of a logical Boolean operation


Java has a bitwise and a logical-AND operation on Booleans, that is, & and &&. Similarly, Java has a bitwise and a logical-OR operation | and ||. The difference is that the logical operations have a short circuit semantics, that is, if the evaluation of the left-hand side is enough to determine the outcome of the operation, then the right-hand side is not evaluated; the bitwise operations, instead, evaluate both sides, always, which might be incorrect is most cases, or at least inefficient.

Action: Use the logical (short-circuit) version of the operators on Booleans.

Examples


Consider the following code:

public class Main {
  public static void main(String[] args) {
    if (args.length == 0 | isOption(args[0]))
      System.out.println("option expected");
    else {
      ....
    }
  }

  private static boolean isOption(String s) {
    ....
  }
}

This checker issues the following warning:

Main.java:3: [ShortCircuit: NonShortCircuitORWarning] Suspicious use of | instead of ||

since the expression isOption(args[0]) is always evaluated, also when args is the empty array. This would result in an ArrayIndexOutOfBoundsException.

In this example, the programmer should replace | with ||.