Index: clangd/CodeComplete.cpp =================================================================== --- clangd/CodeComplete.cpp +++ clangd/CodeComplete.cpp @@ -538,16 +538,14 @@ // Set if the qualifier is not fully resolved by Sema. Optional UnresolvedQualifier; - // Construct scopes being queried in indexes. + // Construct scopes being queried in indexes. The results are deduplicated. // This method format the scopes to match the index request representation. std::vector scopesForIndexQuery() { - std::vector Results; - for (StringRef AS : AccessibleScopes) { - Results.push_back(AS); - if (UnresolvedQualifier) - Results.back() += *UnresolvedQualifier; - } - return Results; + std::set Results; + for (StringRef AS : AccessibleScopes) + Results.insert( + ((UnresolvedQualifier ? *UnresolvedQualifier : "") + AS).str()); + return {Results.begin(), Results.end()}; } }; Index: unittests/clangd/CodeCompleteTests.cpp =================================================================== --- unittests/clangd/CodeCompleteTests.cpp +++ unittests/clangd/CodeCompleteTests.cpp @@ -1142,6 +1142,23 @@ UnorderedElementsAre("")))); } +TEST(CompletionTest, NoDuplicatedQueryScopes) { + auto Requests = captureIndexRequests(R"cpp( + namespace {} + + namespace na { + namespace {} + namespace nb { + ^ + } // namespace nb + } // namespace na + )cpp"); + + EXPECT_THAT(Requests, + ElementsAre(Field(&FuzzyFindRequest::Scopes, + UnorderedElementsAre("na::", "na::nb::", "")))); +} + TEST(CompletionTest, NoIndexCompletionsInsideClasses) { auto Completions = completions( R"cpp(