Skip to content

Extend TernaryOperatorsShouldNotBeNested to support null safe switch expressions #157

Open
@pstreef

Description

@pstreef

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions