diff --git a/clang-tools-extra/clangd/refactor/tweaks/PopulateSwitch.cpp b/clang-tools-extra/clangd/refactor/tweaks/PopulateSwitch.cpp --- a/clang-tools-extra/clangd/refactor/tweaks/PopulateSwitch.cpp +++ b/clang-tools-extra/clangd/refactor/tweaks/PopulateSwitch.cpp @@ -166,7 +166,10 @@ std::string Text; for (EnumConstantDecl *Enumerator : EnumD->enumerators()) { - if (ExistingEnumerators.contains(Enumerator->getInitVal())) + // Try to insert this Enumerator into the set. If this fails, the value was + // either already there to begin with or we have already added it using a + // different name. + if (!ExistingEnumerators.insert(Enumerator->getInitVal()).second) continue; Text += "case "; diff --git a/clang-tools-extra/clangd/unittests/TweakTests.cpp b/clang-tools-extra/clangd/unittests/TweakTests.cpp --- a/clang-tools-extra/clangd/unittests/TweakTests.cpp +++ b/clang-tools-extra/clangd/unittests/TweakTests.cpp @@ -2980,6 +2980,12 @@ void function() { switch (ns::A) {case ns::A:break;} } )"", }, + { + // Selection on duplicate token enum + Function, + R""(enum Enum {A,B,b=B}; ^switch (A) {})"", + R""(enum Enum {A,B,b=B}; switch (A) {case A:case B:break;})"", + }, }; for (const auto &Case : Cases) {