openrewrite / openrewrite/rewrite-static-analysis

Extend TernaryOperatorsShouldNotBeNested to support null safe switch expressions

Open
#157 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Java
Stars
62
Forks
112
Avg merge
1d 19h
Merged PRs (30d)
40

Description

What problem are you trying to solve?

switch(null) is not supported before Java 18. Adding it to this recipe when using java 18 might be a useful extension.

Describe the solution you'd like

"""
  import java.util.Objects;
  class Test {
    public String determineSomething(String a, String b) {
      return Objects.equals(a, "a") ? "a" : Objects.equals(a, "b") ? b : "nope";
    }
  }
  """,
"""
  class Test {
    public String determineSomething(String a, String b) {
      return switch (a) {
          case "a" -> "a";
          case "b" -> b;
          case null -> "nope";
          default -> "nope";
      };
    }
  }
  """

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the TernaryOperatorsShouldNotBeNested recipe and its existing tests. Review how the recipe handles nested ternaries, then use the Java 18 example in this issue to define the null-safe switch-expression case. Done means the recipe recognizes this pattern and the relevant tests cover the expected transformation.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.