[clang-tools-extra] 30f07f1 - [clangd] Show member names in structured binding inlay hints (#245943)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Dec 5 10:20:08 PST 2026
Author: Bryan Sumartono
Date: 2026-12-05T21:20:03+03:00
New Revision: 30f07f1613f711a3059af9f47dd1e8264f680adc
URL: https://github.com/llvm/llvm-project/commit/30f07f1613f711a3059af9f47dd1e8264f680adc
DIFF: https://github.com/llvm/llvm-project/commit/30f07f1613f711a3059af9f47dd1e8264f680adc.diff
LOG: [clangd] Show member names in structured binding inlay hints (#245943)
Show which member each structured binding refers to. For example,
`auto [.name=n: string, age: int] = person;`. The hint is omitted when
the binding has the same name as the member.
The hint is placed before the binding's name, consistent with the
existing
designator hints for aggregate initialization. It is controlled by the
existing `InlayHints.Designators` option.
Fixes clangd/clangd#2727
Assisted-by: Claude Code
Added:
Modified:
clang-tools-extra/clangd/InlayHints.cpp
clang-tools-extra/clangd/Protocol.h
clang-tools-extra/clangd/unittests/InlayHintTests.cpp
clang-tools-extra/docs/ReleaseNotes.md
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/InlayHints.cpp b/clang-tools-extra/clangd/InlayHints.cpp
index e982c2c2f19747..8d0580fffa19cf 100644
--- a/clang-tools-extra/clangd/InlayHints.cpp
+++ b/clang-tools-extra/clangd/InlayHints.cpp
@@ -630,6 +630,9 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
!Type.isNull() && !Type->isDependentType())
addTypeHint(Binding->getLocation(), Type.getCanonicalType(),
/*Prefix=*/": ");
+
+ if (Cfg.InlayHints.Designators)
+ addBindingDesignatorHint(Binding);
}
return true;
}
@@ -1104,6 +1107,21 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
/*Prefix=*/"", Text, /*Suffix=*/"=");
}
+ // For a binding to a data member (e.g. `auto [n, a] = Person{}`), show the
+ // name of the member it refers to before the binding (`.name=n`), unless the
+ // binding has the same name.
+ void addBindingDesignatorHint(BindingDecl *Binding) {
+ // getBinding() is a MemberExpr for data members, but not for tuple-like or
+ // array bindings, or binding packs.
+ auto *ME = dyn_cast_if_present<MemberExpr>(Binding->getBinding());
+ if (!ME)
+ return;
+ auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl());
+ if (!FD || !FD->getIdentifier() || FD->getName() == Binding->getName())
+ return;
+ addDesignatorHint(Binding->getLocation(), ("." + FD->getName()).str());
+ }
+
bool shouldPrintTypeHint(llvm::StringRef TypeName) const noexcept {
return Cfg.InlayHints.TypeNameLimit == 0 ||
TypeName.size() < Cfg.InlayHints.TypeNameLimit;
diff --git a/clang-tools-extra/clangd/Protocol.h b/clang-tools-extra/clangd/Protocol.h
index e81603a7b1a35b..847d9399cdd25d 100644
--- a/clang-tools-extra/clangd/Protocol.h
+++ b/clang-tools-extra/clangd/Protocol.h
@@ -1754,6 +1754,8 @@ enum class InlayHintKind {
/// A hint before an element of an aggregate braced initializer list,
/// indicating what it is initializing.
/// Pair{^1, ^2};
+ /// Also before a structured binding, indicating the member it refers to.
+ /// auto [^a, ^b] = Pair{1, 2};
/// Uses designator syntax, e.g. `.first:`.
/// This is a clangd extension.
Designator = 3,
diff --git a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
index ed074a3d7a0d6b..df606d8d8a74df 100644
--- a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
+++ b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
@@ -1908,6 +1908,102 @@ TEST(DesignatorHints, ParenInitTemplate) {
ExpectedHint{".ptr=", "ptr"});
}
+TEST(DesignatorHints, StructuredBindings) {
+ assertDesignatorHints(R"cpp(
+ struct Person {
+ const char *name;
+ int age;
+ const char *email;
+ };
+ Person p;
+ auto [$n[[n]], $a[[a]], $e[[e]]] = p;
+ // No hints when every binding name matches its field.
+ void f() { auto [name, age, email] = p; }
+ // No hint when the binding name matches its field, but one when swapped.
+ auto &[name, $email[[email]], $age[[age]]] = p;
+ )cpp",
+ ExpectedHint{".name=", "n"}, ExpectedHint{".age=", "a"},
+ ExpectedHint{".email=", "e"},
+ ExpectedHint{".age=", "email"},
+ ExpectedHint{".email=", "age"});
+}
+
+TEST(DesignatorHints, StructuredBindings_FieldKinds) {
+ assertDesignatorHints(R"cpp(
+ struct Base { int x, y; };
+ struct Derived : Base {};
+ struct Bits { int a : 3; int : 5; int b : 5; };
+ struct Refs { int &r; const int &cr; };
+ struct Mutable { mutable int m; };
+ void f(Derived d, Bits b, Refs r, const Mutable cm) {
+ auto [$x[[x1]], $y[[y1]]] = d;
+ auto [$a[[a1]], $b[[b1]]] = b;
+ auto [$r[[r1]], $cr[[cr1]]] = r;
+ auto &[$m[[m1]]] = cm;
+ }
+ // Packs have no hint.
+ template <typename = void> void g(Base b) {
+ auto [$first[[first]], ...rest] = b;
+ }
+ )cpp",
+ ExpectedHint{".x=", "x"}, ExpectedHint{".y=", "y"},
+ ExpectedHint{".a=", "a"}, ExpectedHint{".b=", "b"},
+ ExpectedHint{".r=", "r"}, ExpectedHint{".cr=", "cr"},
+ ExpectedHint{".m=", "m"}, ExpectedHint{".x=", "first"});
+}
+
+TEST(DesignatorHints, StructuredBindings_NotMembers) {
+ // Bindings that don't refer to a named data member get no hint.
+ assertDesignatorHints(R"cpp(
+ // Minimal tuple protocol, so TupleLike is decomposed with get<I>()
+ // rather than by its members.
+ namespace std {
+ template <typename> struct tuple_size;
+ template <unsigned, typename> struct tuple_element;
+ }
+ struct TupleLike {
+ int a, b;
+ template <unsigned I> int get() const { return I ? b : a; }
+ };
+ template <> struct std::tuple_size<TupleLike> {
+ static constexpr unsigned value = 2;
+ };
+ template <unsigned I> struct std::tuple_element<I, TupleLike> {
+ using type = int;
+ };
+
+ struct Point { int x, y; };
+ template <typename T> void dependent(T t) { auto [u, v] = t; }
+
+ void f(TupleLike t) {
+ int arr[2];
+ auto [a1, a2] = arr; // array
+ auto [t1, t2] = t; // tuple-like
+ dependent(Point{}); // dependent, not resolved in the template
+ }
+ )cpp");
+}
+
+TEST(InlayHints, StructuredBindingsTypeAndDesignator) {
+ Annotations Source(R"cpp(
+ struct Point { int x, y; };
+ auto [$a[[a]], $b[[b]]] = Point{};
+ )cpp");
+ auto AST = TestTU::withCode(Source.code()).build();
+ Config Cfg;
+ Cfg.InlayHints.DeducedTypes = true;
+ Cfg.InlayHints.Designators = true;
+ WithContextValue WithCfg(Config::Key, std::move(Cfg));
+ auto Hints = inlayHints(AST, std::nullopt, DefaultOptsForTests);
+ // The designator goes before the binding's name and the type after it.
+ EXPECT_THAT(
+ Hints,
+ ElementsAre(HintMatcher(ExpectedHint{".x=", "a", Left}, Source),
+ HintMatcher(ExpectedHint{": int", "a", Right}, Source),
+ HintMatcher(ExpectedHint{".y=", "b", Left}, Source),
+ HintMatcher(ExpectedHint{": int", "b", Right}, Source)));
+}
+
TEST(InlayHints, RestrictRange) {
Annotations Code(R"cpp(
auto a = false;
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index 2549bb564cdd57..abb05a9cd60f26 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -67,6 +67,11 @@ infrastructure are described first, followed by tool-specific sections.
#### Inlay hints
+- Structured bindings to data members now show which member each binding
+ refers to, e.g. `auto [.name=n, .age=a] = person;`. The hint is omitted when
+ the binding has the same name as the member. It is controlled by the
+ `InlayHints.Designators` configuration option.
+
#### Diagnostics
- Query-based custom clang-tidy checks can now be enabled with the
More information about the cfe-commits
mailing list