Search code examples
javaintellij-ideaassert

Difference between Object.var- - and Object.var-=1?


Today I encountered a strange warning by my IDE, for one piece of code and no warning for another (supposedly identical) code. The following piece of code says the if condition is always false:

public void foo(Node n) {
    assert(n.var > 0);
    n.var--;
    if (n.var == 0) {
         // do stuff
    }
}

While the following works without any warnings:

public void foo(Node n) {
    assert(n.var > 0);
    n.var -= 1;
    if (n.var == 0) {
         // do stuff
    }
}

With the following class:

public class Node {
    private int var;
}

While the first warning is obviously nonsense, I was wondering if there is actually a difference between var-- and var-=1..? And is this actually a warning specific to my IDE? If not, tell me in the comments and I will edit this post accordingly.


Solution

  • It's a bug in IntelliJ IDEA. I've filed it as https://youtrack.jetbrains.com/issue/IDEA-151355, you can watch it to be notified of its progress.