Calling Optional#isPresent() in single line is reported as not called

  • A+
Category:Languages

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 want 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?

 


Sonarqube cannot guarantee that the two calls to id.asInteger() returns the same object, e.g. because multi-threading might have changed the value of id between the two calls, so it is correctly stating that the presence hasn't been adequately tested.

Change code to assign to a local variable first, to ensure that isPresent() and get() are called on the same object:

private boolean isValidId(Id id) {     Optional<Integer> idAsInteger = id.asInteger();     return idAsInteger.isPresent() && idAsInteger.get() >= BASE_ID; } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: