SonarQube server v10.8 (100206)
SonarQube for IDE 10.14.1.80220
Here, var
becomes Boolean
. Sonar then complains about use of a boxed boolean in the if statement, even though it is not possible for it be null. No matter how the Optional is created/mapped/filtered, if the chain ends with an orElse(boolean)
, then it cannot be null, even though the return type is Boolean
instead of the preferred boolean
var notNullable = Optional.ofNullable(null).orElse(false);
if (notNullable) {}
Of course, I could fix this by changing from var
to boolean
. But it shouldn’t be necessary.
For example, if I add an unnecessary null check, then Sonar doesn’t complain:
var notNullable = Optional.ofNullable(null).orElse(false);
if (notNullable == null) return;
if (notNullable) {}