# Java 21 pattern matching false positives

**URL:** <https://community.sonarsource.com/t/java-21-pattern-matching-false-positives/110879>\
**Category:** Report False-positive / False-negative...\
**Tags:** java\
**Created:** [March 11, 2024, 11:15am UTC](https://community.sonarsource.com/t/java-21-pattern-matching-false-positives/110879 "2024-03-11T11:15:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kyris](https://avatars.discourse-cdn.com/v4/letter/k/c4cdca/32.png) [@kyris](https://community.sonarsource.com/u/kyris)\
**Post date:** [March 11, 2024, 11:15am UTC](https://community.sonarsource.com/t/java-21-pattern-matching-false-positives/110879/1 "2024-03-11T11:15:20Z")

</div>

- ALM used: GitHub
- CI system used: GHA
- Languages used: Java 21

With the following code:

```java
sealed interface Animal permits Cat, Dog {}
record Cat() implements Animal { }
record Dog() implements Animal { }

public static int getLives(Animal animal) {
    return switch (animal) {
        case Cat ignored -> 7;
        case Dog ignored -> 1;
    }
}

```

SonarCloud will complain that:

1. **java:S131 Add a default case for this switch**. Thanks to sealed interfaces this pattern match is exhaustive and enforced by the compiler, so no default case is required.
2. **java:S1481 Remove this unused “ignored” local variable**. It cannot be removed (unless using Java 21 preview, in which case it could be replaced by an underscore).
3. **java:S1301 Replace this “switch” statement by “if” statements to increase readability.** With a switch over a sealed interface we gain exhaustive matching, which can be preferable over ifs.

Also, this is not shown in the example above, but if we had a case statement with an empty body (e.g. `case Something something -> { }`, SonarCloud would complain that:

1. **java:S108 Either remove or fill this block of code.** With exhaustive pattern matching it is not possible to remove this case statement - and filling the block of code depends on the use case.

---

<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:** [March 11, 2024, 11:23am UTC](https://community.sonarsource.com/t/java-21-pattern-matching-false-positives/110879/2 "2024-03-11T11:23:40Z")

</div>

Hey there.

I’ve moved your post to the section on reporting false-positives. Can you update your post with the specific Rule IDs concerned? Thanks!

> [@How to Report a False-positive / False-negative](https://community.sonarsource.com/t/how-to-report-a-false-positive-false-negative/37022):
>
> Hey SonarSource Community! [False-positives happen](https://blog.sonarsource.com/false-positives-our-enemies-but-maybe-your-friends), as do false-negatives, and we’re eager to fix them. We are thrilled when our users report problems, so we can make our products better. What is a false-positive (FP)? A false-positive is when an issue is raised unexpectedly on code that should not trigger an issue, or where the suggested action doesn’t make any sense for the code. What is a false-negative (FN)? A false-negative is when an issue should be raised on a piece of code, but isn’t. …

---

<div class="post-metadata">

**Author:** ![kyris](https://avatars.discourse-cdn.com/v4/letter/k/c4cdca/32.png) [@kyris](https://community.sonarsource.com/u/kyris)\
**Post date:** [March 11, 2024, 11:26am UTC](https://community.sonarsource.com/t/java-21-pattern-matching-false-positives/110879/3 "2024-03-11T11:26:46Z")

</div>

Hi Colin, updated my original post. Thank you!

---

<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:** [March 11, 2024, 12:02pm UTC](https://community.sonarsource.com/t/java-21-pattern-matching-false-positives/110879/4 "2024-03-11T12:02:03Z")

</div>

Thanks! One ticket already exists.

> [@kyris](#):
>
> 1. **java:S1301 Replace this “switch” statement by “if” statements to increase readability.** With a switch over a sealed interface we gain exhaustive matching, which can be preferable over ifs.

[SONARJAVA-4461](https://sonarsource.atlassian.net/browse/SONARJAVA-4461)

I’ll refer the other cases to our team of experts. Stay tuned!

---

<div class="post-metadata">

**Author:** ![Michael](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/michael/32/43_2.png) [@Michael](https://community.sonarsource.com/u/Michael)\
**Post date:** [March 13, 2024, 2:34pm UTC](https://community.sonarsource.com/t/java-21-pattern-matching-false-positives/110879/7 "2024-03-13T14:34:35Z")

</div>

Hey @kyris ,

Thanks a lot for the feedback, it is spot on as we are working these days on improving our support of Java 21. I created multiple tickets thanks to you. Thanks, @Colin, for identifying the first one as well!

Here are the tickets and comments related to what you reported:

- [**java:S108**](https://next.sonarqube.com/sonarqube/coding_rules?open=java%3AS108&rule_key=java%3AS108): Either remove or fill this block of code

- [**java:S131**](https://next.sonarqube.com/sonarqube/coding_rules?open=java%3AS131&rule_key=java%3AS131): “switch” statements should have “default” clauses

- [**java:S1481**](https://next.sonarqube.com/sonarqube/coding_rules?open=java%3AS1481&rule_key=java%3AS1481): Unused local variables should be removed

- [**java:S1301**](https://next.sonarqube.com/sonarqube/coding_rules?open=java%3AS1301&rule_key=java%3AS1301): “switch” statements should have at least 3 “case” clauses

Cheers,  
Michael

---

<div class="post-metadata">

**Author:** ![kyris](https://avatars.discourse-cdn.com/v4/letter/k/c4cdca/32.png) [@kyris](https://community.sonarsource.com/u/kyris)\
**Post date:** [March 13, 2024, 3:09pm UTC](https://community.sonarsource.com/t/java-21-pattern-matching-false-positives/110879/9 "2024-03-13T15:09:15Z")

</div>

Thank you Michael, wasn’t aware about the comment thing in S108!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/78532_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [March 22, 2024, 1:21pm UTC](https://community.sonarsource.com/t/java-21-pattern-matching-false-positives/110879/12 "2024-03-22T13:21:19Z")

</div>

Closed on March 22, 2024, 1:21pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
