SonarLint rule squid S3655: "Optional value should only be accessed after calling isPresent()" does not hold up

Using SonarLint IntelliJ 4.3.0.3495 in IntelliJ IDEA Ultimate 2019.2.4 (Build #IU-192.7142.36).

SonarLint rule squid S3655: “Optional value should only be accessed after calling isPresent()” does not hold up.

Please see this code to reproduce:

import java.util.Optional;
import java.util.function.UnaryOperator;

public enum SonarLintBugDemonstration implements UnaryOperator<String> {
  SONARLINT_PROBLEM_1(Optional.of(false)) {
    @Override
    public String apply(String str) {
      if(SONARLINT_PROBLEM_1.stripWhitespaces.isPresent() && SONARLINT_PROBLEM_1.stripWhitespaces.get()) {
        str = str.replaceAll("\\s", "");
      }
      return str;
    }
  },

  SONARLINT_PROBLEM_2(Optional.of(true)) {
    @Override
    public String apply(String str) {
      if(SONARLINT_PROBLEM_2.stripWhitespaces.isPresent()) {
        if(SONARLINT_PROBLEM_2.stripWhitespaces.get()) str = str.replaceAll("\\s", "");
      }
      return str;
    }
  },

  SONARLINT_PROBLEM_3(Optional.of(true)) {
    @Override
    public String apply(final String str) {
      return (SONARLINT_PROBLEM_3.stripWhitespaces.isPresent() && SONARLINT_PROBLEM_3.stripWhitespaces.get() ? str.replaceAll("\\s", "") : str);
    }
  },

  SONARLINT_PROBLEM_4(Optional.of(true)) {
    @Override
    public String apply(final String str) {
      return (SONARLINT_PROBLEM_4.stripWhitespaces.isPresent() ?
              (SONARLINT_PROBLEM_4.stripWhitespaces.get() ? str.replaceAll("\\s", "") : str) : str);
    }
  };

  private Optional<Boolean> stripWhitespaces;

  SonarLintBugDemonstration(final Optional<Boolean> stripWhitespaces) {
    this.stripWhitespaces = Optional.ofNullable(stripWhitespaces).orElse(Optional.empty());
  }
}

SonarLint will report the warning on all Optional#get() calls.

This is my first bug report, so I apologize if I’m missing anything.

Hello @oliveryasuna,

Welcome in this community and thank you for reporting this!

This is indeed a false positive that we were recently made aware of, for which there is an existing ticket: https://jira.sonarsource.com/browse/SONARJAVA-3235?jql=status%20%3D%20Open%20AND%20text%20~%20"optional".

At this point, however, I cannot tell you when we will fix it. In any case, thank you for reporting it as well as for the reproducer!