Sonarlint is not reporting issue java:S3655 in intellij

Please provide

  • Operating system:
macOS Monterey version 12.5.1
  • SonarLint plugin version:
8.1.0.65508
  • Programming language you’re coding in:
Java

And a thorough description of the problem / question:

Sonarlint is failing to report issue java:S3655 (Optional Value should only be accessed after calling isPresent()) and Sonarqube server is reporting this error on below code.

if (accessControlEntity.getAccountId().isPresent()
          && accessControlAdminService.isBlocked(accessControlEntity.getAccountId().get())) {
        return true;
      }

I tried removing below line locally and sonarlint still not thorwing issue with the snippet, However Sonarqube is throwing it as a code smell

accessControlEntity.getAccountId().isPresent()

IntelliJ Version

IntelliJ IDEA 2022.2 (Community Edition)
Build #IC-222.3345.118, built on July 26, 2022
1 Like

Hi @Udham_Singh,

I can not reproduce. Can you confirm that the following code report a SonarLint issue java:S3655 in your IDE:

  String reproducer(java.util.function.Supplier<java.util.Optional<String>> supplier) {
    if (supplier.get().isPresent()) {
      return supplier.get().get(); // SonarLint issue S3655: Call "Optional#i sPresent0" or "IOptional #isEmpty 0" before accessing the value.
    }
    return "";
  }

and replacing by the following code should remove the issue:

  String reproducer(java.util.function.Supplier<java.util.Optional<String>> supplier) {
    Optional<String> optionalValue = supplier.get();
    if (optionalValue.isPresent()) {
      return optionalValue.get(); // No SonarLint issue S3655
    }
    return "";
  }

For now, my assumption is the java analyzer failed to resolve in your code the semantic of accessControlEntity.getAccountId(). It does not know it returns an Optional.

Hello @alban.auzeill
Yes what you said is correct and same is reported by sonarlint as well.

@alban.auzeill Any idea on work around for this one ?