Optional
value should only be accessed after calling isPresent()
, this rule reports violation even if isPresent()
is checked for true in ternary operator.
x.isPresent() ? x.get() : null
Optional
value should only be accessed after calling isPresent()
, this rule reports violation even if isPresent()
is checked for true in ternary operator.
x.isPresent() ? x.get() : null
Hi Madhav,
I’m not able to reproduce with the latest version of the java analyzer.
Our unit test for rule S3655 passes with:
String get1(Optional<String> a) {
return a.isPresent() ? a.get() : ""; // Compliant
}
String get2(Optional<String> a) {
return a.isPresent() ? "" : a.get(); // Noncompliant
}
Are you on SonarCloud/SonarQube? Which version of SonarQube?