From ba93dfaee90860556678ea4b40d614225ac29bf7 Mon Sep 17 00:00:00 2001 From: "Roman.Ivanov" Date: Mon, 21 Jan 2019 16:27:31 +0700 Subject: [PATCH] EnhancedSwitchBackwardMigration: do not generate break if cannot may complete normally: IDEA-205607 --- ...nhancedSwitchBackwardMigrationInspection.java | 6 +++++- .../afterSwitchStatements3.java | 16 ++++++++++++++++ .../beforeSwitchStatements3.java | 13 +++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/inspection/switchExpressionBackwardMigration/afterSwitchStatements3.java create mode 100644 java/java-tests/testData/inspection/switchExpressionBackwardMigration/beforeSwitchStatements3.java diff --git a/java/java-impl/src/com/intellij/codeInspection/EnhancedSwitchBackwardMigrationInspection.java b/java/java-impl/src/com/intellij/codeInspection/EnhancedSwitchBackwardMigrationInspection.java index a5470ae07795..ae0bf7465f3b 100644 --- a/java/java-impl/src/com/intellij/codeInspection/EnhancedSwitchBackwardMigrationInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/EnhancedSwitchBackwardMigrationInspection.java @@ -270,6 +270,7 @@ public class EnhancedSwitchBackwardMigrationInspection extends AbstractBaseJavaL @Override String generateExpressionBranch(@NotNull PsiStatement statement, CommentTracker ct) { + if (statement instanceof PsiThrowStatement) return ct.text(statement); return "return " + ct.text(statement); } } @@ -315,7 +316,10 @@ public class EnhancedSwitchBackwardMigrationInspection extends AbstractBaseJavaL @Override String generateExpressionBranch(@NotNull PsiStatement statement, CommentTracker ct) { - return ct.text(statement) + "\nbreak;"; + if (ControlFlowUtils.statementMayCompleteNormally(statement)) { + return ct.text(statement) + "\nbreak;"; + } + return ct.text(statement); } @Override diff --git a/java/java-tests/testData/inspection/switchExpressionBackwardMigration/afterSwitchStatements3.java b/java/java-tests/testData/inspection/switchExpressionBackwardMigration/afterSwitchStatements3.java new file mode 100644 index 000000000000..10444afbd41a --- /dev/null +++ b/java/java-tests/testData/inspection/switchExpressionBackwardMigration/afterSwitchStatements3.java @@ -0,0 +1,16 @@ +// "Replace with old style 'switch' statement" "true" +import java.util.*; + +class X { + boolean x(int i) { + boolean b; + switch (i) { + case 2: + throw null; + default: + b = true; + break; + } + return b; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/switchExpressionBackwardMigration/beforeSwitchStatements3.java b/java/java-tests/testData/inspection/switchExpressionBackwardMigration/beforeSwitchStatements3.java new file mode 100644 index 000000000000..6636e84a32a9 --- /dev/null +++ b/java/java-tests/testData/inspection/switchExpressionBackwardMigration/beforeSwitchStatements3.java @@ -0,0 +1,13 @@ +// "Replace with old style 'switch' statement" "true" +import java.util.*; + +class X { + boolean x(int i) { + boolean b; + switch (i) { + case 2 -> throw null; + default -> b = true; + } + return b; + } +} \ No newline at end of file