diff --git a/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp b/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp --- a/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp +++ b/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp @@ -397,6 +397,14 @@ if (auto *MD = llvm::dyn_cast(Source)) { if (MD->getParent()->isTemplated()) return false; + + // The refactoring is meaningless for unnamed classes. + const auto *Parent = MD->getParent(); + while (Parent) { + if (Parent->getName().empty()) + return false; + Parent = llvm::dyn_cast_or_null(Parent->getParent()); + } } // Note that we don't check whether an implementation file exists or not in diff --git a/clang-tools-extra/clangd/unittests/tweaks/DefineOutlineTests.cpp b/clang-tools-extra/clangd/unittests/tweaks/DefineOutlineTests.cpp --- a/clang-tools-extra/clangd/unittests/tweaks/DefineOutlineTests.cpp +++ b/clang-tools-extra/clangd/unittests/tweaks/DefineOutlineTests.cpp @@ -84,6 +84,23 @@ template void fo^o() {}; template <> void fo^o() {}; )cpp"); + + // Not available on methods of unnamed classes. + EXPECT_UNAVAILABLE(R"cpp( + struct Foo { + struct { void b^ar() {} } Bar; + }; + )cpp"); + + // Not available on methods of named classes with unnamed parent in parents + // nesting. + EXPECT_UNAVAILABLE(R"cpp( + struct Foo { + struct { + struct Bar { void b^ar() {} }; + } Baz; + }; + )cpp"); } TEST_F(DefineOutlineTest, FailsWithoutSource) {