squid:S3655 (Optional#isPresent() before accessing the value)

Apr '20 - I am experiencing an issue where if “.isPresent()” is called as part of the return value of a method nested in an outer class (e.g. someObject.makeAnOptionalObject().isPresent()), the sonar cube rules do not recognize the “.isPresent()” call.

Hello @krausce,

I know the question is already quite old, did you manage to find an answer your question?

From what I understand, you are facing something looking like this:

if(someObject.makeAnOptionalObject().isPresent()) {
   someObject.makeAnOptionalObject().get(); // False positive?
}

To me, this is not a FP. In fact, the call to makeAnOptionalObject() could return something different at each call. If you put the result of the call in a variable before performing the check, the issue should disappear.

If something is still not clear, feel free to come back with a complete example.

Best,
Quentin

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.