mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-05-06 05:10:22 +07:00
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:
@@ -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);
|
||||
|
||||
@@ -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'
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user