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”).