Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DLS_DEAD_LOCAL_STORE issue in Java 21 with pattern matching #2878

Open
CamilYed opened this issue Feb 28, 2024 · 2 comments
Open

DLS_DEAD_LOCAL_STORE issue in Java 21 with pattern matching #2878

CamilYed opened this issue Feb 28, 2024 · 2 comments

Comments

@CamilYed
Copy link

CamilYed commented 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.


sealed interface Result {

    record Success() implements Result {
    }

    record Failure(String message) implements Result  {
    }

}

  void handle(Result result) {
      switch(result) {
          case Result.Success ignored -> { log.info ("Success"); }
          case Result.Failure failure -> { log.error ("Failure {}", failure.message()); }
      }

  }

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.

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

Copy link

welcome bot commented Feb 28, 2024

Thanks for opening your first issue here! 😃
Please check our contributing guideline. Especially when you report a problem, make sure you share a Minimal, Complete, and Verifiable example to reproduce it in this issue.

@CamilYed 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
@gtoison
Copy link
Contributor

gtoison commented Feb 28, 2024

Hello, I think this is the same issue as #2736
This should be fixed by #2828 once it is released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants