squid:S3655 (Optional value should only be accessed after calling isPresent()) being fired even though control will never reach the optional.get() call

(Rounak Das) #1

We’re using SonarQube server Version 6.7.5 (build 38563). With default settings & the sonar-way quality profile.

We have a lot of code like this:

Optional<SomeClass> optional = ...

if (optional.isEmpty()) {
     throw new Exception("Error");

var value = optional.get();

This triggers S3655 every time & we have to mark it as a false positive. From this topic I see that this PR was supposed to fix this issue. But from the examples in the PR I think that the case where control flow is interrupted due to the isEmpty() check is missed.

Is there any workaround for this? Or a planned fix?