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

**URL:** <https://community.sonarsource.com/t/sonarlint-rule-squid-s3655-optional-value-should-only-be-accessed-after-calling-ispresent-does-not-hold-up/17005>\
**Category:** IntelliJ Platform\
**Tags:** intellij, java\
**Created:** [November 24, 2019, 4:27pm UTC](https://community.sonarsource.com/t/sonarlint-rule-squid-s3655-optional-value-should-only-be-accessed-after-calling-ispresent-does-not-hold-up/17005 "2019-11-24T16:27:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![oliveryasuna](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/oliveryasuna/32/42142_2.png) [@oliveryasuna](https://community.sonarsource.com/u/oliveryasuna)\
**Post date:** [November 24, 2019, 4:27pm UTC](https://community.sonarsource.com/t/sonarlint-rule-squid-s3655-optional-value-should-only-be-accessed-after-calling-ispresent-does-not-hold-up/17005/1 "2019-11-24T16:27:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Guillaume\_Dequenne](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/guillaume_dequenne/32/7472_2.png) [@Guillaume\_Dequenne](https://community.sonarsource.com/u/Guillaume_Dequenne)\
**Post date:** [November 25, 2019, 8:02am UTC](https://community.sonarsource.com/t/sonarlint-rule-squid-s3655-optional-value-should-only-be-accessed-after-calling-ispresent-does-not-hold-up/17005/2 "2019-11-25T08:02:11Z")

</div>

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"](https://jira.sonarsource.com/browse/SONARJAVA-3235?jql=status%20%3D%20Open%20AND%20text%20~%20%22optional%22).

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!
