mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
[java-highlighting] IDEA-324439 Check duplication in switch with taking into account enum class
GitOrigin-RevId: 5d01e0d205c8db48f46fb6301664324f67373c65
This commit is contained in:
committed by
intellij-monorepo-bot
parent
f92fe33afa
commit
b43535b550
@@ -310,10 +310,23 @@ public class SwitchBlockHighlightingModel {
|
||||
for (Map.Entry<Object, Collection<PsiElement>> entry : values.entrySet()) {
|
||||
if (entry.getValue().size() <= 1) continue;
|
||||
Object duplicateKey = entry.getKey();
|
||||
MultiMap<PsiEnumConstant, PsiReferenceExpression> referencesByEnums = new MultiMap<>();
|
||||
for (PsiElement duplicateElement : entry.getValue()) {
|
||||
if (duplicateElement instanceof PsiReferenceExpression referenceExpression &&
|
||||
referenceExpression.resolve() instanceof PsiEnumConstant enumConstant) {
|
||||
referencesByEnums.putValue(enumConstant, referenceExpression);
|
||||
continue;
|
||||
}
|
||||
HighlightInfo.Builder info = createDuplicateInfo(duplicateKey, duplicateElement);
|
||||
results.add(info.create());
|
||||
}
|
||||
for (Map.Entry<PsiEnumConstant, Collection<PsiReferenceExpression>> references : referencesByEnums.entrySet()) {
|
||||
if (references.getValue().size() <= 1) continue;
|
||||
for (PsiReferenceExpression referenceToEnum : references.getValue()) {
|
||||
HighlightInfo.Builder info = createDuplicateInfo(duplicateKey, referenceToEnum);
|
||||
results.add(info.create());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
public class EnumDuplicates {
|
||||
|
||||
sealed interface IN {}
|
||||
enum T1 implements IN { A, B, C; }
|
||||
enum T2 implements IN { A, B, C, D; }
|
||||
int testDuplicates1(IN i) {
|
||||
return switch (i) {
|
||||
case T1.A -> 1;
|
||||
case T1.B -> 2;
|
||||
case T1.C -> 3;
|
||||
case T2.A-> 4;
|
||||
case T2.B -> 4;
|
||||
case T2.C -> 5;
|
||||
default -> 6;
|
||||
};
|
||||
}
|
||||
|
||||
int testDuplicates2(T1 e) {
|
||||
return switch(e) {
|
||||
case <error descr="Duplicate label 'A'">T1.A</error> -> 1;
|
||||
case <error descr="Duplicate label 'A'">A</error> -> 1;
|
||||
case T1.B -> 2;
|
||||
case T1.C -> 3;
|
||||
};
|
||||
}
|
||||
int testDuplicates3(T1 e) {
|
||||
return switch(e) {
|
||||
case A -> 1;
|
||||
case T1.B -> 2;
|
||||
case T1.C -> 3;
|
||||
};
|
||||
}
|
||||
|
||||
int testDuplicates4(IN e) {
|
||||
return switch(e) {
|
||||
case <error descr="Cannot resolve symbol 'A'">A</error> -> 1;
|
||||
case T1.B -> 2;
|
||||
case T1.C -> 3;
|
||||
};
|
||||
}
|
||||
|
||||
int testDuplicates5(T1 e) {
|
||||
return switch (e) {
|
||||
case <error descr="Duplicate label 'A'">A</error> -> 1;
|
||||
case <error descr="Duplicate label 'A'">A</error> -> 1;
|
||||
case B -> 2;
|
||||
case C -> 3;
|
||||
};
|
||||
}
|
||||
|
||||
int testDuplicates6(T1 e) {
|
||||
return switch (e) {
|
||||
case A -> 1;
|
||||
case B -> 2;
|
||||
case C -> 3;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ class JavaSwitchExpressionsHighlightingTest : LightJavaCodeInsightFixtureTestCas
|
||||
fun testComplexTernaryInSwitch() = doTest()
|
||||
fun testQualifiedEnumInSwitch() = IdeaTestUtil.withLevel(module, LanguageLevel.JDK_21) { doTest() }
|
||||
fun testConstantAssignment() = IdeaTestUtil.withLevel(module, LanguageLevel.JDK_21) { doTest() }
|
||||
fun testEnumDuplicates() = IdeaTestUtil.withLevel(module, LanguageLevel.JDK_21) { doTest() }
|
||||
fun testRedundantCastInSwitchBranch() {
|
||||
myFixture.enableInspections(RedundantCastInspection())
|
||||
doTest()
|
||||
|
||||
Reference in New Issue
Block a user