# java:S3655 false positive?

**URL:** <https://community.sonarsource.com/t/java-s3655-false-positive/79850>\
**Category:** Report False-positive / False-negative...\
**Tags:** false-positive, java, sonarqube-cloud\
**Created:** [January 6, 2023, 8:02am UTC](https://community.sonarsource.com/t/java-s3655-false-positive/79850 "2023-01-06T08:02:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Starnose](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@Starnose](https://community.sonarsource.com/u/Starnose)\
**Post date:** [January 6, 2023, 8:02am UTC](https://community.sonarsource.com/t/java-s3655-false-positive/79850/1 "2023-01-06T08:02:36Z")

</div>

Code snippet -

```auto
if (resp.responseBody().isPresent()) {
    var responseBody = MAPPER.readValue(resp.responseBody().get(), ClientCreationResponse.class);

```

This is triggering java:S3655 :

> Call “Optional#isPresent()” or “!Optional#isEmpty()” before accessing the value.[Why is this an issue?](https://sonarcloud.io/project/issues)

I’m calling “isPresent” right there in the preceding line! This has triggered in a few places in a recent changeset of mine. There are also two like this -

```auto
if (resp.responseBody().isEmpty()) {
    throw new CustomException("Exception message");
}
var responseObject = mapper.readValue(resp.responseBody().get(), ResponseObject.class);

```

Seems safe to me?

---

<div class="post-metadata">

**Author:** ![Colin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/colin/32/55255_2.png) [@Colin](https://community.sonarsource.com/u/Colin)\
**Post date:** [January 9, 2023, 10:58am UTC](https://community.sonarsource.com/t/java-s3655-false-positive/79850/2 "2023-01-09T10:58:54Z")

</div>

Hey there.

I believe a discussion of this rule (and this case) has taken place in this thread:

> [@java:S3655 Call "Optional#isPresent()" before accessing the value](https://community.sonarsource.com/t/java-s3655-call-optional-ispresent-before-accessing-the-value/25526/2):
>
> This is not a false positive. Your code is as follow: if (obj.getOptional().isPresent()) { obj.getOptional().get() // issue } There is no guarantee that the first getOptional() method execution returns the same object as the second getOptional() method execution. You have to store the value locally: Optional\<String\> value = obj.getOptional() if (value.isPresent()) { value.get() // no issue }

---

<div class="post-metadata">

**Author:** ![Starnose](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@Starnose](https://community.sonarsource.com/u/Starnose)\
**Post date:** [January 10, 2023, 3:14am UTC](https://community.sonarsource.com/t/java-s3655-false-positive/79850/3 "2023-01-10T03:14:45Z")

</div>

Thanks Colin,

I think this is a very borderline case to flag as a ‘bug’ category error. As we are happy the underlying code is not going to be switching out the return value of this accessor method, I’m marking this lot as “won’t fix”

I wonder if every instance of code like this triggers a bug?

```auto
if (obj.fieldAccessor() != null) {
  obj.fieldAccessor().doSomething()
}

```
