The following program on IntelliJ
public static void main(String[] args) {
int i = 0;
if (i <= 2) {
System.out.println("ok");
}
}
warns me "Condition 'i <= 2' is always 'true'".
If I replace the condition with i > 2
, I get "Condition 'i > 2' is always 'false'". Same with i == 2
.
But if I replace it with i >= 2
I don't have any warnings.
Why in the last case IntelliJ does not warn me that this condition is always false ?
I'm using IntelliJ 14.1.5 and the compiler used is javac in its 1.8.0_51 version.
Like Paul Boddington said in a comment, it's actually something JetBrains forgot to implement in their algorithm, that has been fixed a few days ago.
Before:
if (opSign == LT && comparedWith <= rangeMin) return alwaysFalse(instruction, runner, memState);
if (opSign == LT && comparedWith > rangeMax) return alwaysTrue(instruction, runner, memState);
if (opSign == LE && comparedWith >= rangeMax) return alwaysTrue(instruction, runner, memState);
if (opSign == GT && comparedWith >= rangeMax) return alwaysFalse(instruction, runner, memState);
if (opSign == GT && comparedWith < rangeMin) return alwaysTrue(instruction, runner, memState);
if (opSign == GE && comparedWith <= rangeMin) return alwaysTrue(instruction, runner, memState);
After:
if (opSign == LT && comparedWith <= rangeMin) return alwaysFalse(instruction, runner, memState);
if (opSign == LT && comparedWith > rangeMax) return alwaysTrue(instruction, runner, memState);
if (opSign == LE && comparedWith >= rangeMax) return alwaysTrue(instruction, runner, memState);
if (opSign == LE && comparedWith < rangeMin) return alwaysFalse(instruction, runner, memState);
if (opSign == GT && comparedWith >= rangeMax) return alwaysFalse(instruction, runner, memState);
if (opSign == GT && comparedWith < rangeMin) return alwaysTrue(instruction, runner, memState);
if (opSign == GE && comparedWith <= rangeMin) return alwaysTrue(instruction, runner, memState);
if (opSign == GE && comparedWith > rangeMax) return alwaysFalse(instruction, runner, memState);
Corresponding issue: https://youtrack.jetbrains.com/issue/IDEA-146950
This was brought to my attention about a week ago so its funny I saw it on here soon after too. I looked into it and its been happening as early as Intellij 10 apparently. There were a few bug fixes for it throughout all the updates but the problem has not been fixed clearly.
Here is a link where Jetbrains refers to the bug. There are multiple cases. IDEA-84489
If its bothering you, try suppressing the warning via Alt+Enter while your cursor is over it and choose the option that says "suppress for..."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With