Java: avoid infinite loop when creating switch branches for enum with duplicate constants (IDEA-357561)

GitOrigin-RevId: e9c84396786baceb58501a56d5d7e529f5ae9276
This commit is contained in:
Bas Leijdekkers
2024-08-22 15:27:14 +00:00
committed by intellij-monorepo-bot
parent c983a3af19
commit e2e6809274
3 changed files with 26 additions and 2 deletions
@@ -1,4 +1,4 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.siyeh.ig.fixes;
import com.intellij.psi.*;
@@ -25,7 +25,7 @@ public final class CreateEnumMissingSwitchBranchesFix extends CreateMissingSwitc
@Override
protected @NotNull List<String> getAllNames(@NotNull PsiClass aClass, @NotNull PsiSwitchBlock switchBlock) {
return StreamEx.of(aClass.getAllFields()).select(PsiEnumConstant.class).map(PsiField::getName).toList();
return StreamEx.of(aClass.getFields()).select(PsiEnumConstant.class).map(PsiField::getName).distinct().toList();
}
@Override
@@ -0,0 +1,15 @@
// "Create missing branches 'A', 'B', and 'C'" "true"
class Main {
enum E {A, B, C, C}
public void f(E e) {
switch (e) {
case A -> {
}
case B -> {
}
case C -> {
}
}
}
}
@@ -0,0 +1,9 @@
// "Create missing branches 'A', 'B', and 'C'" "true"
class Main {
enum E {A, B, C, C}
public void f(E e) {
switch<caret> (e) {
}
}
}