False Positive in Methods return value

Hi team,

We got an issue with sonar. It shows that we always return the same value but we do not.
As the screenshot below, we return 2 different boolean values (true/false).


Hey there.

Please provide an unredacted snippet of code that can reproduce the issue. A screenshot/description is not quite enough.

Hi @Colin ,

Here is the code snippet with redacted sensitive info.

Hey there.

We really need text, not a screenshot.

Here is the code.

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
      throws Exception {
    if (!idempotencyService.isRequiredIdempotentCheckRequest(request)) {    
        return true;
    }
    String uId = channelRequestHolder.getUId();
    UUID idemKey = channelRequestHolder.getIdempotentKey();
    validateMissingIdemKey(idemKey);
    Optional<TransferIdempotencyRequest> previousRequestOpt =
        idempotencyService.findDuplicatedRequest(idemKey, uId);
    if (previousRequestOpt.isPresent()) {
      TransferIdempotencyRequest previousRequest = previousRequestOpt.get();
      if (!channelRequestHolder.isRequestEqualTo(previousRequest)) {
        //multiple requests with different parameters but the same idem key
        throw new IdempotentException(ErrorCode.PARAMETERS_MISMATCH);
      }
      //return memoized response directly without processing more
      writeMemoizedResponse(response, previousRequest);

return false;
    }
    
return true;
  }
}

Hi @asd_asdasd,
Thank you for sharing this bit of code as text, it makes our job a lot easier this way.

S3516 is a symbolic execution rule that sometimes is confused when evaluating possible execution paths that are conditioned with Optional.isPresent().

Unfortunately, I am having a hard time reproducing the issue with the code you shared in this thread as I am missing some of the logic (fields, methods, external classes, library versions, …)

Could you share a self-contained piece of code that produces the same false positive in your project?

Best,

Dorian