# False Positive on \`optional.orElse(null)\`

**URL:** <https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410>\
**Category:** Report False-positive / False-negative...\
**Tags:** false-positive, sonarqube, java\
**Created:** [January 26, 2023, 8:07am UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410 "2023-01-26T08:07:10Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![AJ8GH](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/aj8gh/32/41319_2.png) [@AJ8GH](https://community.sonarsource.com/u/AJ8GH)\
**Post date:** [January 26, 2023, 8:07am UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410/1 "2023-01-26T08:07:10Z")

</div>

- Version info:  
Developer Edition, Version 9.4 (build 54424), [LGPL v3](https://www.gnu.org/licenses/lgpl-3.0.txt)

- 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:

```auto
    Optional.of("anything")
        .map(String::toUpperCase)
        .orElse(null);

```

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

```auto
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:

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

```

---

<div class="post-metadata">

**Author:** ![ganncamp](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ganncamp/32/1261_2.png) [@ganncamp](https://community.sonarsource.com/u/ganncamp)\
**Post date:** [January 27, 2023, 3:10pm UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410/2 "2023-01-27T15:10:52Z")

</div>

Hi,

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?

&nbsp;  
Ann

---

<div class="post-metadata">

**Author:** ![mbrown10](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/mbrown10/32/42869_2.png) [@mbrown10](https://community.sonarsource.com/u/mbrown10)\
**Post date:** [March 8, 2023, 4:21pm UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410/3 "2023-03-08T16:21:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ganncamp](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ganncamp/32/1261_2.png) [@ganncamp](https://community.sonarsource.com/u/ganncamp)\
**Post date:** [March 8, 2023, 4:24pm UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410/5 "2023-03-08T16:24:04Z")

</div>

Hi @mbrown10,

Welcome to the community and thanks for chiming in here.

I’ve flagged this for team attention.

&nbsp;  
Ann

---

<div class="post-metadata">

**Author:** ![jbeleites](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/jbeleites/32/11750_2.png) [@jbeleites](https://community.sonarsource.com/u/jbeleites)\
**Post date:** [March 15, 2023, 9:27am UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410/7 "2023-03-15T09:27:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![henrykuijpers](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/henrykuijpers/32/44017_2.png) [@henrykuijpers](https://community.sonarsource.com/u/henrykuijpers)\
**Post date:** [April 11, 2023, 11:20am UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410/9 "2023-04-11T11:20:02Z")

</div>

We currently have this issue as well:

![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/b/f/bf4923a7a80e174005758134a0cf53a2c488fd5b.png)

java:S2637 is the issue that is reported.

Using SonarQube Developer Edition Version 9.5 (build 56709).

---

<div class="post-metadata">

**Author:** ![henrykuijpers](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/henrykuijpers/32/44017_2.png) [@henrykuijpers](https://community.sonarsource.com/u/henrykuijpers)\
**Post date:** [April 11, 2023, 11:22am UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410/10 "2023-04-11T11:22:35Z")

</div>

Java’s `Optional.ofNullable`’s first parameter, however, is annotated with `@Nullable`.

---

<div class="post-metadata">

**Author:** ![jbeleites](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/jbeleites/32/11750_2.png) [@jbeleites](https://community.sonarsource.com/u/jbeleites)\
**Post date:** [April 11, 2023, 12:54pm UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410/12 "2023-04-11T12:54:28Z")

</div>

Thank you @henrykuijpers for confirming the issue.

After digging a bit more, in fact, [we have a ticket](https://sonarsource.atlassian.net/browse/SONARJAVA-4121) for this issue. Thank you all for reporting the issue anyway, it helps us assess how prevalent the problem is and how to prioritize fixing it.

---

<div class="post-metadata">

**Author:** ![henrykuijpers](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/henrykuijpers/32/44017_2.png) [@henrykuijpers](https://community.sonarsource.com/u/henrykuijpers)\
**Post date:** [April 12, 2023, 8:23am UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410/14 "2023-04-12T08:23:57Z")

</div>

Do you have any idea how I can get my atlassian account to join the SonarSource JIRA? I see that the linked issue has been there for more than a year already, without a fix. If needed, I can try to work on this, to get this fixed.

---

<div class="post-metadata">

**Author:** ![henrykuijpers](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/henrykuijpers/32/44017_2.png) [@henrykuijpers](https://community.sonarsource.com/u/henrykuijpers)\
**Post date:** [April 12, 2023, 8:29am UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410/15 "2023-04-12T08:29:06Z")

</div>

Do you have any idea on how to join the SonarSource JIRA with my Atlassian account?  
I see that the linked ticket is more than 1 year old, without a fix. It would be nice to work on this.

---

<div class="post-metadata">

**Author:** ![ganncamp](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ganncamp/32/1261_2.png) [@ganncamp](https://community.sonarsource.com/u/ganncamp)\
**Post date:** [April 12, 2023, 12:25pm UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410/16 "2023-04-12T12:25:56Z")

</div>

Hi,

Unfortunately, you won’t be able to interact in our Jira projects. Fortunately, you don’t need to to contribute. Feel free to reference an existing ticket in a PR and if we accept the PR, we’ll handle the Jira admin.

&nbsp;  
😃  
Ann

---

<div class="post-metadata">

**Author:** ![henrykuijpers](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/henrykuijpers/32/44017_2.png) [@henrykuijpers](https://community.sonarsource.com/u/henrykuijpers)\
**Post date:** [May 3, 2023, 9:17am UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410/17 "2023-05-03T09:17:39Z")

</div>

Hi, is there any progress on the issue? And/or are there any pointers on how to fix this? It’s popping up very frequently and making our quality pipelines fail.

I would be willing to submit a PR, however, I have no idea where to start on trying to make a test case to prove the issue and then fixing it.

---

<div class="post-metadata">

**Author:** ![henrykuijpers](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/henrykuijpers/32/44017_2.png) [@henrykuijpers](https://community.sonarsource.com/u/henrykuijpers)\
**Post date:** [January 17, 2024, 1:59pm UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410/18 "2024-01-17T13:59:58Z")

</div>

@ganncamp Any update on this issue? Would love to see this fixed. And I could also help fixing it, if I knew how.

---

<div class="post-metadata">

**Author:** ![ganncamp](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ganncamp/32/1261_2.png) [@ganncamp](https://community.sonarsource.com/u/ganncamp)\
**Post date:** [January 17, 2024, 2:05pm UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410/19 "2024-01-17T14:05:48Z")

</div>

@henrykuijpers if there’s no progress in Jira, then there’s no progress.

&nbsp;  
Ann

---

<div class="post-metadata">

**Author:** ![henrykuijpers](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/henrykuijpers/32/44017_2.png) [@henrykuijpers](https://community.sonarsource.com/u/henrykuijpers)\
**Post date:** [March 24, 2025, 9:03am UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410/20 "2025-03-24T09:03:34Z")

</div>

@ganncamp This was supposed to be fixed in v7.15 (according to the history @Michael set this fix version). Later on, we read somewhere that this was fixed in a 9.9 version.

We are currently running version 2025.1 and we still see this issue occurring.

Any ETA on the fix being pushed through?

---

<div class="post-metadata">

**Author:** ![ganncamp](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/ganncamp/32/1261_2.png) [@ganncamp](https://community.sonarsource.com/u/ganncamp)\
**Post date:** [March 24, 2025, 11:59am UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410/21 "2025-03-24T11:59:56Z")

</div>

Hi @henrykuijpers,

Again, if there’s no progress in Jira, there’s no progress. This ticket is still `Open`.

&nbsp;  
Ann

---

<div class="post-metadata">

**Author:** ![henrykuijpers](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/henrykuijpers/32/44017_2.png) [@henrykuijpers](https://community.sonarsource.com/u/henrykuijpers)\
**Post date:** [March 24, 2025, 12:16pm UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410/22 "2025-03-24T12:16:39Z")

</div>

@ganncamp And it’s still impossible to contribute the fix?

We use this pattern a lot and it’s very annoying that our quality gate fails all the time; We then have to mark the issue as “false positive” and then rerun the build (and quality gate).

---

<div class="post-metadata">

**Author:** ![Leonardo\_Pilastri](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/leonardo_pilastri/32/38853_2.png) [@Leonardo\_Pilastri](https://community.sonarsource.com/u/Leonardo_Pilastri)\
**Post date:** [March 25, 2025, 9:44am UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410/24 "2025-03-25T09:44:32Z")

</div>

Hi Henry, as Ann mentioned the ticket is still open and it does not have priority over others. That being said if you are familiar with the analyzer and you have a fix, please feel free to create a PR!  
The ticket is fairly detailed and should give some directions already.

The “Contributing” section of sonar-java ([here](https://github.com/SonarSource/sonar-java?tab=readme-ov-file#contributing)) should kickstart you with running tests and creating a PR, but let me know if I can assist you with that.

---

<div class="post-metadata">

**Author:** ![Andret2344](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/andret2344/32/77160_2.png) [@Andret2344](https://community.sonarsource.com/u/Andret2344)\
**Post date:** [August 7, 2026, 9:59pm UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410/28 "2026-08-07T21:59:38Z")

</div>

If anyone needs it, and I bet there are individuals who do, here’s my PR with a fix for that:

> <https://github.com/SonarSource/sonar-java/pull/5906>
>
> Fixes the false positive reported in SONARJAVA-4121 (and its twin JAVASE-87).
> 
> D…iscussed in https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410
> 
> \## Problem
> 
> \`\`\`java
> import java.util.Optional;
> import org.eclipse.jdt.annotation.NonNull;
> 
> class A {
> private final String value = "value";
> 
> @NonNull // when this is removed, the FP disappears
> public String getValue() {
> return value;
> }
> 
> public String test(A a) {
> return Optional.ofNullable(a)
> .map(A::getValue)
> .orElse(null); // FP: Parameter 1 to this call is marked "@NonNull" but null could be passed
> }
> }
> \`\`\`
> 
> \`org.eclipse.jdt.annotation.NonNull\` and \`lombok.NonNull\` are \`TYPE\_USE\` annotations, so ECJ keeps them in
> the type binding. In the snippet above the type argument is inferred as \`@NonNull String\`, the receiver
> becomes \`Optional\<@NonNull String\>\` and the substituted signature of \`orElse\` becomes
> \`@NonNull String orElse(@NonNull String)\`. With \`javax.annotation.Nonnull\`, which is not \`TYPE\_USE\`, the
> FP does not appear.
> 
> The frontend copied those inferred annotations onto the symbols of the call:
> 
> \* \`JMethodSymbol#declarationParameters\` built the parameter placeholder from the substituted parameter
> type, so \`JSymbolMetadata#of\` stored \`@NonNull\` among the annotations of the parameter. The parameter
> was then reported as \`NON\_NULL\` at \`VARIABLE\` level, which is what S2637 reads.
> \* \`JSymbol#convertMetadata\` used the substituted return type, so the same annotation ended up on the
> method symbol. \`Optional#orElse\` was reported as \`NON\_NULL\` at \`METHOD\` level, which makes the
> symbolic execution engine constrain the result of the call to \`NOT\_NULL\`.
> 
> Note that the annotation only leaks when the type variable is substituted directly. For \`map\`, whose
> return type is \`Optional\<@NonNull String\>\`, the annotation correctly stays in the type argument metadata.
> 
> \## Fix
> 
> Annotations are read from the declared types, while \`type()\` and \`returnType()\` keep the substituted
> types, as checks rely on them. Annotations written explicitly on a declaration, including those on type
> arguments of a declared parameter, are unaffected; only annotations inferred at the call site are
> dropped.
> 
> \* \`JMethodSymbol\` and \`JVariableSymbol\`: parameter metadata comes from the declared parameter type
> \* \`JSymbol\`: method metadata comes from the declared return type
> 
> \## Tests
> 
> \`NullabilityWithInferredTypeArgument\` reproduces the reported snippet and the null check that the leaked
> return type annotation would turn into an always-false condition. Two tests in \`JSymbolMetadataTest\`
> cover the parameter and the method side; both fail without the change:
> 
> \`\`\`
> Expecting empty but was: \[org.sonar.java.model.JSymbolMetadata$JAnnotationInstance@5dac488d\]
> \`\`\`
> 
> Ran on JDK 26: \`java-frontend\` (1595 tests), \`java-checks-testkit\` (210 tests) and \`java-checks\`
> (1706 tests) show no new failures compared to master.

I hope it’ll be accepted soon!

---

<div class="post-metadata">

**Author:** ![gabriel.fleischer](https://avatars.discourse-cdn.com/v4/letter/g/f07891/32.png) [@gabriel.fleischer](https://community.sonarsource.com/u/gabriel.fleischer)\
**Post date:** [August 26, 2026, 8:44am UTC](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410/35 "2026-08-26T08:44:10Z")

</div>

Hello,

Thank you for your proposition, it looks promising !

We plan to work on Java Symbolic Execution Engine in the coming weeks and we will try to merge your PR.

Best

[Next page](https://community.sonarsource.com/t/false-positive-on-optional-orelse-null/82410.md?page=2)
