FP javabugs:S6651 - presumes modulus result will be positive

  • What language is this for? Java
  • Which rule? javabugs:S6651: Conditionally executed code should be reachable
  • Why do you believe it’s a false-positive/false-negative? The condition really can resolve to true or false
  • Are you using
    • SonarQube Server / Community Build - which version? v2025.1 (102418)
  • How can we reproduce the problem? Give us a self-contained snippet of code

In this example, both inputs are expected to be positive, but we do not know which value will be larger.

If aLong < anInt then the value of diff will be negative.
But SonarQube seems to believe it will always be positive.

  long method(int anInt, long aLong) {
    long diff = (aLong - anInt) % 3;
    return diff < 0 ? diff + 3 : diff;
  }

I have no inside knowledge in the Java analyzer of Sonar, but I agree that this code does not assume the outcome of the modulo to be positive. Hence this is a FP.

Hi Chris,

I confirm that this is an FP.

You are right, this seems to be caused by a bug in our internal modeling of the remainder operator which leads to us inferring that the result of the operation is always non-negative.

We are now tracking the FP, and I will update you on the status.

Best regards,

Anton