[Narrowed down] False positive for rule java:S2583

Operating system: Windows 10
SonarLint plugin version: 8.5.1.75093
Programming language: Java

I have narrowed down this false positive to the following code:

import java.util.Scanner;

public class Main {
    public static final double VALUE1 = 0.7;
    public static final double VALUE2 = 0.9;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        double val = scanner.nextDouble();

        if (val < VALUE1) {
            System.out.println("1");
            return;
        }

        if (val < VALUE2) {
            System.out.println("2");
            return;
        }

        System.out.println("3");
    }
}

Key takeaways:

  • You need two double, static, and final constants.
  • The constants must be smaller than 1

Although passing the first if still allows val to hold a value like 0.8, SonarLint claims it is impossible, and raises a java:S2583 issue on the 2nd if’s condition (“val < VALUE2”).

Hello @Deathcofi,

Thanks for your message. I was able to reproduce the issue in the latest SonarLint plugin. However, the issue isn’t present if I use the latest build of the Java analyzer.

Looks like the issue was fixed by this ticket: [SONARJAVA-4592] - Jira

I’d suggest waiting till the next version of the analyzer is released and appears in SonarLint so you can confirm it’s fixed.

Regards,
Margarita