Improve analysis for null checks

  • SonarQube: Data Center Edition Version 8.6 (build 39681)
  • Code similar to the following raises a possible error:
import com.google.common.base.Preconditions;
public class Validator{
  private static void validate(String s) {
    Preconditions.checkArgument(!Objects.isNull(s), "must be not null");
    System.out.println(s.length()); // message is here
  }
}

I get a message like:

A "NullPointerException" could be thrown; "s" is nullable here.

which is not true since the previous line guards against that.

Hey @alshopov ,

This case is supposed to be perfectly handled. On my side, I can not reproduce the issue when guava is present in the dependencies of the project. The only way I managed to reproduce the same issue than you was by manually making our analyzer “not recognize” Preconditions.checkArgument() method. Therefore, it seems to me that you might have a problem of configuration of your project.

Can you check that google’s guava bytecode is available during analysis?
Running your analysis with a debug flag should help you to see if a dependency is missing.

Also, please note that the version 8.6 of SQ is already quite old, and not officially supported anymore. I would recommend you to update to SonarQube 8.8.

Cheers,
Michael