[java-inspections] IDEA-333228 AIOOBE: EnhancedSwitchMigrationInspection.inspectReturningSwitch

- fix case, when `default` is not last

GitOrigin-RevId: 93d12d7a0d4a929c58630a4a6f1f5b2c9a10d039
This commit is contained in:
Mikhail Pyltsin
2023-09-25 11:18:28 +00:00
committed by intellij-monorepo-bot
parent b128380722
commit 0c27b276dc
3 changed files with 36 additions and 1 deletions
@@ -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;
@@ -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";
};
}
}
@@ -0,0 +1,18 @@
// "Replace with 'switch' expression" "true-preview"
class X {
enum State {
CANCELLED, INTERRUPTING, NORMAL
}
String test(State state, String outcome) {
sw<caret>itch (state){
default:
break;
case CANCELLED:
case INTERRUPTED:
return "2";
}
return "1";
}
}