java inspectin: merge switch case label element lists when possible

in "Duplicate branches in 'switch'" inspection fix

GitOrigin-RevId: 7b22a3409f5d9237f432ab3dd35f27cacfbd7737
This commit is contained in:
Bas Leijdekkers
2022-07-20 18:53:31 +00:00
committed by intellij-monorepo-bot
parent f133fbc17f
commit e2bcaddbe3
21 changed files with 39 additions and 42 deletions
@@ -1,4 +1,4 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInspection;
import com.intellij.codeInspection.util.InspectionMessage;
@@ -405,7 +405,24 @@ public final class DuplicateBranchesInSwitchInspection extends LocalInspectionTo
PsiElement prevElement = PsiTreeUtil.skipWhitespacesAndCommentsBackward(myLabelToMergeWith);
if (prevElement != null) moveTarget = prevElement;
}
moveTarget.getParent().addRangeAfter(firstElementToMove, lastElementToMove, moveTarget);
if (PsiUtil.isLanguageLevel14OrHigher(moveTarget) && moveTarget instanceof PsiSwitchLabelStatement) {
final PsiSwitchLabelStatement labelStatement = (PsiSwitchLabelStatement)moveTarget;
final PsiCaseLabelElementList caseLabelElementList = labelStatement.getCaseLabelElementList();
assert caseLabelElementList != null;
for (PsiElement element : myBranchPrefixToMove) {
if (element instanceof PsiSwitchLabelStatement) {
final PsiSwitchLabelStatement statement = (PsiSwitchLabelStatement)element;
final PsiCaseLabelElementList list1 = statement.getCaseLabelElementList();
assert list1 != null;
for (PsiCaseLabelElement labelElement : list1.getElements()) {
caseLabelElementList.addAfter(labelElement, caseLabelElementList.getLastChild());
}
}
}
}
else {
moveTarget.getParent().addRangeAfter(firstElementToMove, lastElementToMove, moveTarget);
}
firstElementToMove.getParent().deleteChildRange(firstElementToMove, lastElementToMove);
}
@@ -15,7 +15,7 @@ and suggests merging the duplicate branches.
System.out.println("default");
}
</code></pre>
<p>Can be replaced with:</p>
<p>After the quick-fix is applied:</p>
<pre><code>
<b>switch</b> (n) {
<b>case</b> 1:
@@ -2,8 +2,7 @@
class C {
void test(int n) {
String s = switch (n) {
case 1:
case 3:
case 1, 3:
break "a";
case 2:
break "b";
@@ -3,8 +3,7 @@ class C {
void test(int n) {
String s = switch (n) {
default:
case 1:
case 3:
case 1, 3:
break "a";
case 2:
break "b";
@@ -4,8 +4,7 @@ class C {
String s = switch (n) {
case 2:
break "b";
case 3:
case 1:
case 3, 1:
default:
break "a";
};
@@ -2,8 +2,7 @@
class C {
int foo(int n, boolean b) {
switch (n) {
case 1:
case 3:
case 1, 3:
if(b) {
return bar("A");
} else {
@@ -2,8 +2,7 @@
class C {
void foo(int n, boolean b) {
switch (n) {
case 1:
case 3:
case 1, 3:
if(b) {
bar("A");
} else {
@@ -4,8 +4,7 @@ class C {
int s = 0;
for (int i = 0; i < n; i++) {
switch (i % 4) {
case 1:
case 3:
case 1, 3:
s += i;
continue;
case 2:
@@ -2,8 +2,7 @@
class C {
void foo(int n) {
switch (n) {
case 1:
case 3:
case 1, 3:
bar("A");
case 2:
break;
@@ -2,8 +2,7 @@
class C {
void foo(int n) {
switch (n) {
case 1:
case 4:
case 1, 4:
bar("A");
case 2:
break;
@@ -2,8 +2,7 @@
class C {
String foo(int n) {
switch (n) {
case 1:
case 2:
case 1, 2:
foo(); // same comment
return "A";
// another comment
@@ -3,8 +3,7 @@ class C {
String foo(int n) {
switch (n) {
/* comment 1 */
case 1:
case 2:
case 1, 2:
/* comment 2 */
return "A"; // comment 3
}
@@ -4,9 +4,7 @@ enum T {
int foo(T t) {
switch (t) {
case A:
case B:
case A, B:
return t.ordinal(); // comment 1
case C:
@@ -2,8 +2,7 @@
class C {
void foo(int n) {
switch (n) {
case 1:
case 3:
case 1, 3:
bar("A");
break;
case 2:
@@ -2,8 +2,7 @@
class C {
String foo(int n) {
switch (n) {
case 1:
case 3:
case 1, 3:
return "A";
case 2:
return "B";
@@ -2,8 +2,7 @@
class C {
String foo(int n) {
switch (n) {
case 1:
case 2:
case 1, 2:
/* comment 1 */
return "A";
}
@@ -3,8 +3,7 @@ class C {
void foo(int n) {
switch (n) {
// comment
case 1:
case 2:
case 1, 2:
bar("A");
break;
}
@@ -2,8 +2,7 @@
class C {
void foo(int n) {
switch (n) {
case 1:
case 3:
case 1, 3:
bar("A");
break;
case 2:
@@ -2,8 +2,7 @@
class C {
void foo(int n) {
switch (n) {
case 1:
case 4:
case 1, 4:
bar("A");
break;
case 2:
@@ -2,8 +2,7 @@
class C {
String foo(int n) {
switch (n) {
case 1:
case 3:
case 1, 3:
throw new IllegalArgumentException("A");
case 2:
throw new IllegalStateException("A");
@@ -3,8 +3,7 @@ class C {
void foo(int n) {
switch (n) {
case 1:
case 2:
case 3:
case 2, 3:
bar("A");
break;
case 4: