Java: Don't offer the action "Replace with old-style switch" on empty 'switch' statement (IDEA-208270)

This commit is contained in:
Pavel Dolgov
2019-03-05 13:13:41 +03:00
parent e501f7a853
commit 800fdfcd8f
2 changed files with 17 additions and 3 deletions
@@ -5,10 +5,10 @@ import com.intellij.codeInsight.BlockUtils;
import com.intellij.codeInsight.daemon.impl.analysis.HighlightUtil;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiTypesUtil;
import com.siyeh.ig.psiutils.CommentTracker;
import com.siyeh.ig.psiutils.ControlFlowUtils;
import com.siyeh.ig.psiutils.SwitchUtils;
import gnu.trove.TIntArrayList;
import one.util.streamex.StreamEx;
import org.jetbrains.annotations.Nls;
@@ -35,7 +35,7 @@ public class EnhancedSwitchBackwardMigrationInspection extends AbstractBaseJavaL
return new JavaElementVisitor() {
@Override
public void visitSwitchExpression(PsiSwitchExpression expression) {
if (!SwitchUtils.isRuleFormatSwitch(expression)) return;
if (!isNonemptyRuleFormatSwitch(expression)) return;
if (findReplacer(expression) == null) return;
String message = InspectionsBundle.message("inspection.switch.expression.backward.expression.migration.inspection.name");
holder.registerProblem(expression.getFirstChild(), message, new ReplaceWithOldStyleSwitchFix());
@@ -43,11 +43,16 @@ public class EnhancedSwitchBackwardMigrationInspection extends AbstractBaseJavaL
@Override
public void visitSwitchStatement(PsiSwitchStatement statement) {
if (!SwitchUtils.isRuleFormatSwitch(statement)) return;
if (!isNonemptyRuleFormatSwitch(statement)) return;
if (findReplacer(statement) == null) return;
String message = InspectionsBundle.message("inspection.switch.expression.backward.statement.migration.inspection.name");
holder.registerProblem(statement.getFirstChild(), message, new ReplaceWithOldStyleSwitchFix());
}
private boolean isNonemptyRuleFormatSwitch(PsiSwitchBlock block) {
PsiSwitchLabelStatementBase label = PsiTreeUtil.getChildOfType(block.getBody(), PsiSwitchLabelStatementBase.class);
return label instanceof PsiSwitchLabeledRuleStatement;
}
};
}
@@ -0,0 +1,9 @@
// "Replace with old style 'switch' statement" "false"
import java.util.*;
class SwitchExpressionMigration {
void foo(int n) {
<caret>switch (n) {
}
}
}