spotbugs / spotbugs

SpotBugs is FindBugs' successor. A tool for static analysis to look for bugs in Java code.

Home Page:https://spotbugs.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

False negative for FL_FLOATS_AS_LOOP_COUNTERS

Han-Cui opened this issue · comments

Hi, I found false negatives for FL_FLOATS_AS_LOOP_COUNTERS with very simple cases.

Version

Spotbugs 4.7.3(the latest version)
Java 11

Source code

public class Test {

    public static void test1() {
        float x = 0.1f;
        while (x < 10) { // TP
            System.out.println(x);
            x++;
        }
    }

    public static void test2() {
        float x = 10.1f;
        while (x > 0) { // FN

            System.out.println(x);
            x--;
        }
    }

    public static void test3() {
        float x = 0.1f;
        while (x < 10) { // FN
            x++;
            System.out.println(x);
        }
    }

    public static void test4() {
        float x = 0.1f;
        while (x > 10) { // TP
            System.out.println(x);
            x++;
        }
    }

    public static void test5() {
        float x = 0.1f;
        while (x < 10) { // TP
            System.out.println(x);
            x--;
        }
    }
}

Outputs

Using floating-point loop counters can lead to unexpected behavior.
At Test.java:[line 5]
In method Test.test1()

Using floating-point loop counters can lead to unexpected behavior.​
At Test.java:[line 30]​
In method Test.test4()

Using floating-point loop counters can lead to unexpected behavior.​
At Test.java:[line 38]​
In method Test.test5()

Description

test1() is a test case from spotbugsTestCases/src/java/DontUseFloatsAsLoopCounters.java, which is obviously a true positive case.
Comparing to test1(), test2() changes from increment of variable x to decrement of variable x, which is still a floating-point loop counter usage. But Spotbugs doesn't give any warning on test2(), which should be a false negative.
test3() exchanges the order between the statement of increment of x and println(x), which is still a floating-point loop counter usage. But Spotbugs doesn't give any warning on test3(), which should be a false negative.
Thanks for your consideration.

Thanks for opening your first issue here! 😃
Please check our contributing guideline. Especially when you report a problem, make sure you share a Minimal, Complete, and Verifiable example to reproduce it in this issue.