S3516: FP when return type is Void

The rule raises an issue when multiple early returns are used in a function returning a Void. For example

public class CallableReproducer {

  public void function(boolean a) throws Exception {
    execute(new Callable<Void>() {

      @Override
      public Void call() throws Exception {
        if (a) {
          return null;
        }
        return null;
      }

    });
  }
  private <T> void execute(Callable<T> callable) throws Exception {
    callable.call();
  }
}

Void is not instantiable, but even if it were, one would always return something like Void.NIL. Runnable is no alternative if the API used expects a Callable<T>.

Hi,

indeed this is a false positive (and quite a corner case, but no reason not to fix it!). Thanks for the feedback and reproducer, ticket created to handle the issue: https://jira.sonarsource.com/browse/SONARJAVA-3155

1 Like