case
标签的 switch
语句和表达式,并建议将它们重写为 if
和 else if
语句。
示例 (minimum branches == 3):
switch (expression) {
case "foo" -> foo();
case "bar" -> bar();
}
在应用快速修复后:
if ("foo".equals(expression)) {
foo();
} else if ("bar".equals(expression)) {
bar();
}
配置检查:
使用最小分支数 字段指定 case
标签的最小预期数量。
Use the Do not report pattern switch statements option to avoid reporting switch statements and expressions that have pattern branches. E.g.:
String result = switch(obj) {
case String str -> str.trim();
default -> "none";
};
It might be preferred to keep the switch even with a single pattern branch, rather than using the instanceof
statement.