diff --git a/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp b/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp --- a/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp +++ b/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp @@ -82,6 +82,15 @@ return false; } +// Returns true iff "auto" in Node is really part of the template parameter, +// which we cannot expand. +bool isTemplateParam(const SelectionTree::Node *Node) { + if (Node->Parent) + if (Node->Parent->ASTNode.get()) + return true; + return false; +} + bool ExpandAutoType::prepare(const Selection& Inputs) { CachedLocation = llvm::None; if (auto *Node = Inputs.ASTSelection.commonAncestor()) { @@ -90,7 +99,8 @@ // Code in apply() does handle 'decltype(auto)' yet. if (!Result.getTypePtr()->isDecltypeAuto() && !isStructuredBindingType(Node) && - !isDeducedAsLambda(Node, Result.getBeginLoc())) + !isDeducedAsLambda(Node, Result.getBeginLoc()) && + !isTemplateParam(Node)) CachedLocation = Result; } } diff --git a/clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp b/clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp --- a/clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp +++ b/clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp @@ -80,6 +80,9 @@ // unknown types in a template should not be replaced EXPECT_THAT(apply("template void x() { ^auto y = T::z(); }"), StartsWith("fail: Could not deduce type for 'auto' type")); + + ExtraArgs.push_back("-std=c++17"); + EXPECT_UNAVAILABLE("template class Y;"); } } // namespace