mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
EnhancedSwitchBackwardMigrationInspection: insert break in statement switch, when may complete normally
This commit is contained in:
+18
-4
@@ -210,12 +210,12 @@ public class EnhancedSwitchBackwardMigrationInspection extends AbstractBaseJavaL
|
||||
String caseValuesText = caseValues == null ? "" : ct.text(caseValues);
|
||||
PsiStatement body = rule.getBody();
|
||||
String finalBody;
|
||||
if (!(body instanceof PsiBlockStatement) && body != null) {
|
||||
if (body == null) {
|
||||
finalBody = "";
|
||||
} else if (!(body instanceof PsiBlockStatement)) {
|
||||
finalBody = generateExpressionBranch(body, ct);
|
||||
} else {
|
||||
finalBody = StreamEx.of(ControlFlowUtils.unwrapBlock(body))
|
||||
.map(el -> ct.text(el))
|
||||
.joining("\n");
|
||||
finalBody = generateBlockBranch(body, ct);
|
||||
}
|
||||
ct.grabComments(rule);
|
||||
|
||||
@@ -224,6 +224,12 @@ public class EnhancedSwitchBackwardMigrationInspection extends AbstractBaseJavaL
|
||||
return prefix + ":" + finalBody;
|
||||
}
|
||||
|
||||
String generateBlockBranch(@NotNull PsiStatement statement, CommentTracker ct) {
|
||||
return StreamEx.of(ControlFlowUtils.unwrapBlock(statement))
|
||||
.map(el -> ct.text(el))
|
||||
.joining("\n");
|
||||
}
|
||||
|
||||
abstract void handleBreakInside(@NotNull PsiBreakStatement breakStatement, CommentTracker ct);
|
||||
|
||||
abstract String generateExpressionBranch(@NotNull PsiStatement statement, CommentTracker ct);
|
||||
@@ -290,5 +296,13 @@ public class EnhancedSwitchBackwardMigrationInspection extends AbstractBaseJavaL
|
||||
String generateExpressionBranch(@NotNull PsiStatement statement, CommentTracker ct) {
|
||||
return ct.text(statement) + "\nbreak;";
|
||||
}
|
||||
|
||||
@Override
|
||||
String generateBlockBranch(@NotNull PsiStatement statement, CommentTracker ct) {
|
||||
if (ControlFlowUtils.statementMayCompleteNormally(statement)) {
|
||||
return super.generateBlockBranch(statement, ct) + "\nbreak;";
|
||||
}
|
||||
return super.generateBlockBranch(statement, ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// "Replace with old style 'switch' statement" "true"
|
||||
import java.util.*;
|
||||
|
||||
public class Main {
|
||||
void foo(E e) {
|
||||
switch (e) {
|
||||
case E1, E2:
|
||||
System.out.println("oops");
|
||||
break;
|
||||
default:
|
||||
System.out.println("impossible");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum E {
|
||||
E1, E2;
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// "Replace with old style 'switch' statement" "true"
|
||||
import java.util.*;
|
||||
|
||||
public class Main {
|
||||
void foo(E e) {
|
||||
switch (e) {
|
||||
case E1, E2:
|
||||
System.out.println("oops");
|
||||
break;
|
||||
default:
|
||||
System.out.println("impossible");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum E {
|
||||
E1, E2;
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// "Replace with old style 'switch' statement" "true"
|
||||
import java.util.*;
|
||||
|
||||
public class Main {
|
||||
void foo(E e) {
|
||||
switch<caret> (e) {
|
||||
case E1, E2 -> {
|
||||
System.out.println("oops");
|
||||
}
|
||||
default -> {
|
||||
System.out.println("impossible");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum E {
|
||||
E1, E2;
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// "Replace with old style 'switch' statement" "true"
|
||||
import java.util.*;
|
||||
|
||||
public class Main {
|
||||
void foo(E e) {
|
||||
switch<caret> (e) {
|
||||
case E1, E2 -> {
|
||||
System.out.println("oops");
|
||||
}
|
||||
default -> {
|
||||
System.out.println("impossible");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum E {
|
||||
E1, E2;
|
||||
}
|
||||
Reference in New Issue
Block a user