You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In our Java codebase, we're utilizing pattern matching introduced in Java 21. While compiling the code, we encountered a "DLS_DEAD_LOCAL_STORE" warning pointing to the parameter named "ignored" within a specific switch case block. Despite its declaration, the "ignored" parameter is not utilized within the associated code block.
sealed interface Result {
record Success() implements Result {
}
record Failure(String message) implements Result {
}
}
We should in the case of a switch block be able to allow the use of a variable, for example, named ignored.
Unnamed pattern variables
An unnamed pattern variable can appear in a type pattern (JLS §14.30.1), including var type patterns, regardless of whether the type pattern appears at the top level or is nested in a record pattern. For example, the Ball example can now be written:
// JEP 456 JDK 22
switch (ball) {
case RedBall _ -> process(ball); // Unnamed pattern variable
case BlueBall _ -> process(ball); // Unnamed pattern variable
case GreenBall _ -> stopProcessing(); // Unnamed pattern variable
}
or
// JDK 21
switch (ball) {
case RedBall ignored -> process(ball); // ignored
case BlueBall ignored-> process(ball); // ignored
case GreenBall ignored -> stopProcessing(); // ignored
}
This is a similar pattern when we catch an exception that we do nothing about.
try {
foo();
} catch(SomeException ignored) {
}
Steps to Reproduce
Compile the provided Java code using Java 21.
Observe the "DLS_DEAD_LOCAL_STORE" warning associated with the "ignored" parameter.
Thank you for your attention to this matter. Please let us know if you need any further information or clarification regarding this issue.
Best regards,
Kamil Jedrzejuk
The text was updated successfully, but these errors were encountered:
CamilYed
changed the title
Dead local store issue in Java 21 with pattern matching
DLS_DEAD_LOCAL_STORE issue in Java 21 with pattern matching
Feb 28, 2024
In our Java codebase, we're utilizing pattern matching introduced in Java 21. While compiling the code, we encountered a "DLS_DEAD_LOCAL_STORE" warning pointing to the parameter named "ignored" within a specific switch case block. Despite its declaration, the "ignored" parameter is not utilized within the associated code block.
Expected Behavior
As long as there are no unnamed variables in Java
https://openjdk.org/jeps/456
We should in the case of a switch block be able to allow the use of a variable, for example, named ignored.
This is a similar pattern when we catch an exception that we do nothing about.
Steps to Reproduce
Compile the provided Java code using Java 21.
Observe the "DLS_DEAD_LOCAL_STORE" warning associated with the "ignored" parameter.
Thank you for your attention to this matter. Please let us know if you need any further information or clarification regarding this issue.
Best regards,
Kamil Jedrzejuk
The text was updated successfully, but these errors were encountered: