DeleteSwitchLabelFix: check again whether the whole branch should be removed when fix is actually applied

Fixes IDEA-200021 Red code after applying 'Fix all 'Constant conditions and exception' problems in the file' with multiple case labels
This commit is contained in:
Tagir Valeev
2018-10-08 13:22:38 +07:00
parent c09250da45
commit 1554b0c513
3 changed files with 28 additions and 6 deletions

View File

@@ -26,14 +26,16 @@ public class DeleteSwitchLabelFix implements LocalQuickFix {
public DeleteSwitchLabelFix(PsiSwitchLabelStatement label) {
myName = Objects.requireNonNull(label.getCaseValue()).getText();
myBranch = shouldRemoveBranch(label);
}
private static boolean shouldRemoveBranch(PsiSwitchLabelStatement label) {
PsiStatement nextStatement = PsiTreeUtil.getNextSiblingOfType(label, PsiStatement.class);
if (nextStatement instanceof PsiSwitchLabelStatement) {
myBranch = false;
}
else {
PsiStatement prevStatement = PsiTreeUtil.getPrevSiblingOfType(label, PsiStatement.class);
myBranch = prevStatement == null || !ControlFlowUtils.statementMayCompleteNormally(prevStatement);
return false;
}
PsiStatement prevStatement = PsiTreeUtil.getPrevSiblingOfType(label, PsiStatement.class);
return prevStatement == null || !ControlFlowUtils.statementMayCompleteNormally(prevStatement);
}
@Nls(capitalization = Nls.Capitalization.Sentence)
@@ -56,7 +58,7 @@ public class DeleteSwitchLabelFix implements LocalQuickFix {
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiSwitchLabelStatement label = PsiTreeUtil.getNonStrictParentOfType(descriptor.getStartElement(), PsiSwitchLabelStatement.class);
if (label == null) return;
if (myBranch) {
if (shouldRemoveBranch(label)) {
PsiCodeBlock scope = ObjectUtils.tryCast(label.getParent(), PsiCodeBlock.class);
if (scope == null) return;
PsiSwitchLabelStatement nextLabel = PsiTreeUtil.getNextSiblingOfType(label, PsiSwitchLabelStatement.class);

View File

@@ -0,0 +1,9 @@
// "Fix all 'Constant conditions & exceptions' problems in file" "true"
class Main {
void t() {
int i = 5;
switch(i) {
// Apply 'Fix all problems in the file'
}
}
}

View File

@@ -0,0 +1,11 @@
// "Fix all 'Constant conditions & exceptions' problems in file" "true"
class Main {
void t() {
int i = 5;
switch(i) {
c<caret>ase 1: case 3: // Apply 'Fix all problems in the file'
System.out.println("odd");
break;
}
}
}