Hello,
I am using SonarQube Version 6.7.4 (build 38452), and found a false positive on rule squid:2583.
This happens when using java.util.Optional with filters, and I get the issue “Change this condition so that it does not always evaluate to “false”” on my “else if” condition.
Here is the minimal code sample I was able to reproduce the bug with :
package test;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.logging.Logger;
/**
* Test S2583
*
* @author vhemery
*/
public final class S2583FalsePositive {
/**
* Private constructor
*/
private S2583FalsePositive() {
// do nothing
}
/**
* Test
*
* @param string Hello!
*/
public static void test(String string) {
Predicate<String> startsWithH = s -> s.startsWith("H");
Predicate<String> endsWithExcl = s -> s.endsWith("!");
Optional<String> a = Optional.ofNullable(string).filter(startsWithH.or(endsWithExcl));
if(a.filter(startsWithH).isPresent()) {
Logger.getGlobal().info("Hello");
} else if(a.filter(endsWithExcl).isPresent()) {
Logger.getGlobal().info("ello!");
/*
* Comment a.get() to remove the false positive in the if above...
*/
a.get();
}
}
}
The strangest part is that the false positive is not triggered when I remove the get instruction under the comment, whereas this should have no impact at all on the condition itself.