Java:S2589 False Negative for Constant Expressions in Boolean Conditions

Rule:

S2589 Boolean expressions should not be gratuitous

Environment:

  • SonarQube Version: 10.0.0.68432

Description:

As shown in the following code sample. It should be reported as noncompliant with rule S2589 because the condition c != 0 will always be false, as c is a constant initialized to 0.

Here’s the code sample that triggers the false negative:

public class booleanExpression {
    public static final int c=0;

    public static void booleanExpressionMethod() {
        if (c != 0) {    // FN; "c" is always "0"
            System.out.println("c == 0");
        }
    }

}

Hi Seren, thank you for the report!
Indeed our engine is failing to evaluate constants outside the method’s scope. I created this ticket to track the issue and to fix it.

Thank you for your reply and for addressing my concern. I appreciate your intention to solve this issue.