Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ - Warning message does not appear for the condition i >= 2 when i is known

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.

enter image description here

like image 209
user2336315 Avatar asked Nov 04 '15 16:11

user2336315


Video Answer


2 Answers

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

like image 96
Bastien Jansen Avatar answered Nov 13 '22 08:11

Bastien Jansen


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..."

like image 30
Rykuno Avatar answered Nov 13 '22 07:11

Rykuno