False Positive on `optional.orElse(null)`

  • Version info:
    Developer Edition, Version 9.4 (build 54424), LGPL v3

  • what are you trying to achieve:

Pass null as an argument to orElse in the Java Optional API: Optional.of(<anything>).map(<anyFunction>).orElse(null)

Example of how to replicate the issue in code:


This is failing as it’s being marked by SonarQube as a bug, with the message:

Parameter 1 to this call is marked "@NotNull" but null could be passed.

I believe this is incorrect and is a false positive, because the parameter itself is marked nullable in the java optional source code:

    public T orElse(@Nullable @Flow(targetIsContainer = true) T other) {
        return value != null ? value : other;


Welcome to the community!

What rule is raising this issue? The id will be something like java:S111. Also, can you upgrade to the latest version, SonarQube 9.8, and see if this is replicable?


I have the same issue using SonarCloud caused by rule java:S2637

1 Like

Hi @mbrown10,

Welcome to the community and thanks for chiming in here.

I’ve flagged this for team attention.


Hello Matt, hello Adam,

Thank you for reporting this issue. Indeed, this looks like a false positive.

Unfortunately, I have not been able to reproduce the issue on SonarCloud or SonarQube based on the first example provided in the initial post. Could you provide a stand-alone compilable reproducer, which triggers this behavior? Optionally, I can open a private channel for you to share such a reproducer if you cannot make it work without including private code.