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

Extend TernaryOperatorsShouldNotBeNested to collapse switch cases

Open pstreef opened this issue 1 year ago • 0 comments

What problem are you trying to solve?

When multiple cases have the same output they are currently all present, they can be collapsed/concentrated into a single case

Describe the solution you'd like

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

pstreef avatar Aug 22 '23 18:08 pstreef