I run SonarQube to check my code and I found a case which I don't understand the reported error.
My code is:
private static final int BASE_ID = 100_000_000;
private boolean isValidId(Id id) {
return id.asInteger().isPresent() && id.asInteger().get() >= BASE_ID;
}
The method asInteger
returns Optional<Integer>
The error that I am getting from sonarqube is
Call "Optional#isPresent()" before accessing the value.
in the return line.
I understand that the code is ok as the second part of the if
won’t get executed if the first one is false. I know that this can be solved with a .filter(..).isPresent()
but I like it more this way.
Any ideas why would this happen?