diff --git a/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp b/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp --- a/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp +++ b/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp @@ -175,6 +175,13 @@ bool AddUsing::prepare(const Selection &Inputs) { auto &SM = Inputs.AST->getSourceManager(); + + // Do not suggest "using" in header files. That way madness lies. + const auto FileName = SM.getFileEntryForID(SM.getMainFileID())->getName(); + if (FileName.endswith(".h") || FileName.endswith(".hpp")) { + return false; + } + auto *Node = Inputs.ASTSelection.commonAncestor(); if (Node == nullptr) return false; 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 @@ -2463,6 +2463,13 @@ // test that we don't crash. EXPECT_UNAVAILABLE(Header + "template using foo = one::tt;"); + + // Check that we do not trigger in header files. + FileName = "test.h"; + ExtraArgs.push_back("-xc++-header"); // .h file is treated a C by default. + EXPECT_UNAVAILABLE(Header + "void fun() { one::two::f^f(); }"); + FileName = "test.hpp"; + EXPECT_UNAVAILABLE(Header + "void fun() { one::two::f^f(); }"); } TEST_F(AddUsingTest, Apply) {