False positive for Java Optional

Sonar Version 6.7.4 (build 38452)
This code is a false positive:

if(foo.bar.isPresent() && foo.bar.get() instanceof Something)

as well as this:

if (foo.bar.isPresent()) {

Hello @dforce,

Thank you for reporting this and my apologies for the delayed answer.

This is indeed a false positive that we weren’t aware of, for which I’ve created the following ticket: https://jira.sonarsource.com/browse/SONARJAVA-3235.

In the meantime, an alternative that might be useful for you depending on your use case would be to avoid using Optional::get in favor of constructs such as:

foo.bar.ifPresent(b -> myMethod(b));

You can find other examples here: https://reversecoding.net/java-8-optional-replace-get-examples/

Hope that helps,