rewrite-static-analysis
rewrite-static-analysis copied to clipboard
Extend TernaryOperatorsShouldNotBeNested to support null safe switch expressions
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";
};
}
}
"""