Checker InfiniteRecursion   as of Julia version 2.6.0 (built on 6 Sep 2018)

belongs to group Basic

Identify calls that look like infinite recursion


This checker identifies very simple cases of infinite recusion, when a method calls itself with exactly the same parameter values. This leads very often to an infinite recursion and is likely to be a programming bug. Note, however, that, in reality, there might not be an actual infinite recursion if the state is modified by the method and termination depends on those changes of state. Although, in those cases, it could still be argued that the programmer is using a bad, unclear programming pattern and should at least clarify the code.

Action: Check if there is an actual possibility of non-termination and correct the recursion accordingly.

Examples


Consider the following program:

public class Loop {
  public static void main(String[] args) {
    System.out.println(new Loop().pow(args.length));
  }

  public int pow(int i) {
    int j = i;
    if (i == 0)
      return 1;
    else
      return 2 * pow(j);
  }
}

This checker issues the following warning:

Loop.java:11: [InfiniteRecursion: InfiniteRecursionWarning] This call seems to induce an infinite recursion

since pow calls itself recursively with exactly the same parameter values, which leads to non-termination. The programmer probably forgot a -1 in the parameter of the recursive call.

In this example, the programmer should correct the recursive call in order to guarantee a decreasing chain of recursive parameter values:

  public int pow(int i) {
    int j = i;
    if (i == 0)
      return 1;
    else
      return 2 * pow(j - 1);
  }