SonarQube claims condition to always evaluate to false for fields accessed with "this."

Viewed 838

This seems to be a new example of a false positive of the rule "Conditionally executed blocks should be reachable" (squid:S2583). Does anyone know why SonarQube claims that if(this.x == 0) always evaluates to false in the following Java class?

public class MyClass {

    private long x;

    void setX(long x) {
        this.x = x;
    }

    public void decrementX() {
        if(this.x > 0) {
            this.x--;
            if(this.x == 0)  { //                 <-- Always false?!
                // apparently dead code
            }
        }
    }
}

Clearly the variable x can be set to 1 and then decrementX() will get into that exact condition:

@Test
public void testDecrement() {
    MyClass c = new MyClass();
    c.setX(1);
    c.decrementX();
}

(executed on SonarQube server 5.6.6 with SonarJava plugin 4.13.0.11627)

Update: as noted by Absurd-Mind, SonarQube is happy when this.x is shortened to x. In my opinion, this is a false-positive.

1 Answers
Related