Suggestion for new rule/bug detection improvement

SonarQube Enterprise Edition Version 9.9.1 (build 69595)

The second line of this Java code is a bug, but it isn’t detected as a bug.

//stop after 600
if (i % 100 == 600) { break; }

Get it? It is just like if (1 == 0)… the conditional always evaluates to false. No matter what value i takes in the bug line, the result can never equal 600 (because % returns the mathematical remainder).

This is the corrected version: if (i % 600 == 0) { break; }, which would actually break after 600 in the context of the original code this bug was pulled from. The bug was created when the developer (me) made an edit on the wrong side of the == in this original (bug free) version: if (i % 100 == 0)

Hey @david.loyall!

On my end, SonarQube raises java:S2583 (Conditionally executed code should be reachable), specifically with the issue message Change this condition so that it does not always evaluate to "false".

So maybe this rule has gotten smarter since v9.9.1, or this rule is inactive in your Quality Profile, or I’m not reproducing it right.

  void sayHello() {
    System.out.println("Hello World!");
    int i = 0;
    while(i < 1000000){
      if (i % 100 == 600) { break; }
    }
  }

Nice. Colin, thanks, I will find out if java:S2583 can be made available on our installation.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.