[java-inspection] IDEA-357079 Convert switch expression to old style switch with boolean selector: do not add default branch

GitOrigin-RevId: c02a9cd74b36adbf1e46c012cce3ef4c490c4eb9
This commit is contained in:
Mikhail Pyltsin
2024-08-02 21:18:28 +02:00
committed by intellij-monorepo-bot
parent f215f6d268
commit d3b39b27a9
4 changed files with 65 additions and 2 deletions

View File

@@ -9,6 +9,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiTypesUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.containers.ContainerUtil;
import com.siyeh.ig.psiutils.CommentTracker;
import com.siyeh.ig.psiutils.ControlFlowUtils;
@@ -211,6 +212,9 @@ public final class EnhancedSwitchBackwardMigrationInspection extends AbstractBas
IntList caseCounts = new IntArrayList();
StringJoiner joiner = new StringJoiner("\n");
boolean addDefaultBranch = mySwitchBlock instanceof PsiSwitchExpression;
if (mySwitchBlock.getExpression() != null) {
addDefaultBranch &= !TypeConversionUtil.isBooleanType(mySwitchBlock.getExpression().getType());
}
for (int i = 0; i < rules.size(); i++) {
PsiSwitchLabeledRuleStatement rule = rules.get(i);
CommentTracker ct = new CommentTracker();
@@ -229,7 +233,6 @@ public final class EnhancedSwitchBackwardMigrationInspection extends AbstractBas
joiner.add(generate);
mainCommentTracker.markUnchanged(rule);
addDefaultBranch &= !SwitchUtils.isDefaultLabel(rule);
}
if (addDefaultBranch) {
joiner.add("default:throw new java.lang.IllegalArgumentException();");

View File

@@ -0,0 +1,34 @@
// "Fix all 'Enhanced 'switch'' problems in file" "true"
import java.util.*;
class SwitchWithDefault {
int booleanSwitch(boolean b) {
switch (b) {
case true:
return 1;
case false:
return 2;
}
}
int booleanSwitch2(Boolean b) {
switch (b) {
case true:
return 1;
case false:
return 2;
}
}
int booleanSwitch3(Boolean b) {
int i;
switch (b) {
case true:
i = 1;
break;
case false:
i = 2;
break;
}
}
}

View File

@@ -0,0 +1,25 @@
// "Fix all 'Enhanced 'switch'' problems in file" "true"
import java.util.*;
class SwitchWithDefault {
int booleanSwitch(boolean b) {
return swit<caret>ch (b) {
case true -> 1;
case false -> 2;
};
}
int booleanSwitch2(Boolean b) {
return switch (b) {
case true -> 1;
case false -> 2;
};
}
int booleanSwitch3(Boolean b) {
int i = switch (b) {
case true -> 1;
case false -> 2;
};
}
}

View File

@@ -4,6 +4,7 @@ package com.intellij.java.codeInspection;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
import com.intellij.codeInspection.EnhancedSwitchBackwardMigrationInspection;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.pom.java.JavaFeature;
import com.intellij.pom.java.LanguageLevel;
import org.jetbrains.annotations.NotNull;
@@ -20,6 +21,6 @@ public class EnhancedSwitchBackwardMigrationInspectionTest extends LightQuickFix
@Override
protected LanguageLevel getLanguageLevel() {
return LanguageLevel.JDK_21_PREVIEW;
return JavaFeature.PRIMITIVE_TYPES_IN_PATTERNS.getMinimumLevel();
}
}