diff --git a/java/java-impl/src/com/intellij/codeInspection/EnhancedSwitchMigrationInspection.java b/java/java-impl/src/com/intellij/codeInspection/EnhancedSwitchMigrationInspection.java index eabc2e7161fb..92b1b8eb974b 100644 --- a/java/java-impl/src/com/intellij/codeInspection/EnhancedSwitchMigrationInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/EnhancedSwitchMigrationInspection.java @@ -486,7 +486,7 @@ public class EnhancedSwitchMigrationInspection extends AbstractBaseJavaLocalInsp statements = psiCodeBlock.getCodeBlock().getStatements(); } if (statements.length == 0) { - if (i == branches.size() - 1) { + if ((i == branches.size() - 1) || branch.isDefault()) { if (returnAfterSwitch != null) { statements = new PsiStatement[]{returnAfterSwitch}; } @@ -497,6 +497,9 @@ public class EnhancedSwitchMigrationInspection extends AbstractBaseJavaLocalInsp return null; } } + else { + return null; + } } if (maxLines < statements.length) { maxLines = statements.length; diff --git a/java/java-tests/testData/inspection/switchExpressionMigration/afterDefaultNotLast.java b/java/java-tests/testData/inspection/switchExpressionMigration/afterDefaultNotLast.java new file mode 100644 index 000000000000..dd394d96ac8d --- /dev/null +++ b/java/java-tests/testData/inspection/switchExpressionMigration/afterDefaultNotLast.java @@ -0,0 +1,14 @@ +// "Replace with 'switch' expression" "true-preview" + +class X { + enum State { + CANCELLED, INTERRUPTING, NORMAL + } + + String test(State state, String outcome) { + return switch (state) { + default -> "1"; + case CANCELLED, INTERRUPTED -> "2"; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/switchExpressionMigration/beforeDefaultNotLast.java b/java/java-tests/testData/inspection/switchExpressionMigration/beforeDefaultNotLast.java new file mode 100644 index 000000000000..136860a7b8d0 --- /dev/null +++ b/java/java-tests/testData/inspection/switchExpressionMigration/beforeDefaultNotLast.java @@ -0,0 +1,18 @@ +// "Replace with 'switch' expression" "true-preview" + +class X { + enum State { + CANCELLED, INTERRUPTING, NORMAL + } + + String test(State state, String outcome) { + switch (state){ + default: + break; + case CANCELLED: + case INTERRUPTED: + return "2"; + } + return "1"; + } +} \ No newline at end of file