Optional.isPresent() chained by && not recognized

Please provide

  • Operating system: Windows 10
  • SonarLint plugin version:
  • Programming language you’re coding in: Java
  • Is connected mode used:
    • Connected to SonarQube Community Edition - Version 8.9.6 (build 50800)
      And a thorough description of the problem / question:

I have a boolean expression chained together by “&&”.

&& dto.getStructure().get().getCompositionType() != null
&& dto.getStructure.get().getCompositionType().getValue() != null

But SonarQube/SonarLint gives me java:S3655 as a major bug, although it is obviously the first check in the chain.
Happens locally aswell as on our deployed version.

My guess is, that this is a bug of Sonar…
Has anyone else experienced this issue?


Our analyzer is not able to detect if dto.getStructure() always returns the same object. It looks like a getter, but in Java this is just convention, and the field behind could possibly be changed by a concurrent thread.

Changing your code to:

var optionalStructure = dto.getStructure();

&& optionalStructure.get().getCompositionType() != null
&& optionalStructure.get().getCompositionType().getValue() != null

will make one step in the right direction, but then I think you are likely to get another issue for a similar reason because of the two consecutive calls of getCompositionType(). The result can be non-null the first time, and null the second time.

So in the end, you have no choice than introducing another intermediate variable. This will make your code more thread safe. Something like:

var optionalStructure = dto.getStructure();

&& hasNonNullValue(optionalStructure.get().getCompositionType())

private boolean hasNonNullValue(@Nullable CompositionType compType) {
  return compType != null && compType.getValue() != null;

And finally, if your plan was to do something with the value itself, you need to refactor again, but I hope you understood the idea :slight_smile:

NB: If you are 100% sure your objects are immutable, you can also mark the issue as “False positive”.