rewrite-static-analysis icon indicating copy to clipboard operation
rewrite-static-analysis copied to clipboard

Extend TernaryOperatorsShouldNotBeNested to support null safe switch expressions

Open pstreef opened this issue 1 year ago • 0 comments

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";
      };
    }
  }
  """

pstreef avatar Aug 22 '23 18:08 pstreef