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 @@ -53,13 +53,24 @@ std::string ExpandAutoType::title() const { return "Expand auto type"; } +// Structured bindings must use auto, e.g. `const auto& [a,b,c] = ...;`. +// Return whether N (an AutoTypeLoc) is such an auto that must not be expanded. +bool isStructuredBindingType(const SelectionTree::Node *N) { + // Walk up the TypeLoc chain, because auto may be qualified. + while (N && N->ASTNode.get()) + N = N->Parent; + // The relevant type is the only direct type child of a Decomposition. + return N && N->ASTNode.get(); +} + bool ExpandAutoType::prepare(const Selection& Inputs) { CachedLocation = llvm::None; if (auto *Node = Inputs.ASTSelection.commonAncestor()) { if (auto *TypeNode = Node->ASTNode.get()) { if (const AutoTypeLoc Result = TypeNode->getAs()) { // Code in apply() does handle 'decltype(auto)' yet. - if (!Result.getTypePtr()->isDecltypeAuto()) + if (!Result.getTypePtr()->isDecltypeAuto() && + !isStructuredBindingType(Node)) CachedLocation = Result; } } 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 @@ -572,6 +572,8 @@ R"cpp(const char * x = "test";)cpp"); EXPECT_UNAVAILABLE("dec^ltype(au^to) x = 10;"); + // expanding types in structured bindings is syntactically invalid. + EXPECT_UNAVAILABLE("const ^auto &[x,y] = (int[]){1,2};"); // FIXME: Auto-completion in a template requires disabling delayed template // parsing.