Skip to content

Commit 87f03e1

Browse files
committed
[clangd] Don't offer to expand auto in structured binding declarations.
auto must be used for the code to parse. Differential Revision: https://reviews.llvm.org/D89700
1 parent 7202344 commit 87f03e1

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,24 @@ REGISTER_TWEAK(ExpandAutoType)
5353

5454
std::string ExpandAutoType::title() const { return "Expand auto type"; }
5555

56+
// Structured bindings must use auto, e.g. `const auto& [a,b,c] = ...;`.
57+
// Return whether N (an AutoTypeLoc) is such an auto that must not be expanded.
58+
bool isStructuredBindingType(const SelectionTree::Node *N) {
59+
// Walk up the TypeLoc chain, because auto may be qualified.
60+
while (N && N->ASTNode.get<TypeLoc>())
61+
N = N->Parent;
62+
// The relevant type is the only direct type child of a Decomposition.
63+
return N && N->ASTNode.get<DecompositionDecl>();
64+
}
65+
5666
bool ExpandAutoType::prepare(const Selection& Inputs) {
5767
CachedLocation = llvm::None;
5868
if (auto *Node = Inputs.ASTSelection.commonAncestor()) {
5969
if (auto *TypeNode = Node->ASTNode.get<TypeLoc>()) {
6070
if (const AutoTypeLoc Result = TypeNode->getAs<AutoTypeLoc>()) {
6171
// Code in apply() does handle 'decltype(auto)' yet.
62-
if (!Result.getTypePtr()->isDecltypeAuto())
72+
if (!Result.getTypePtr()->isDecltypeAuto() &&
73+
!isStructuredBindingType(Node))
6374
CachedLocation = Result;
6475
}
6576
}

clang-tools-extra/clangd/unittests/TweakTests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,8 @@ TEST_F(ExpandAutoTypeTest, Test) {
572572
R"cpp(const char * x = "test";)cpp");
573573

574574
EXPECT_UNAVAILABLE("dec^ltype(au^to) x = 10;");
575+
// expanding types in structured bindings is syntactically invalid.
576+
EXPECT_UNAVAILABLE("const ^auto &[x,y] = (int[]){1,2};");
575577

576578
// FIXME: Auto-completion in a template requires disabling delayed template
577579
// parsing.

0 commit comments

Comments
 (0)