[llvm-branch-commits] [clang-tools-extra] [clangd] Show type hint for simple cases of dependent 'auto' (PR #156284)
Nathan Ridge via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sun Aug 31 23:49:47 PDT 2025
https://github.com/HighCommander4 created https://github.com/llvm/llvm-project/pull/156284
Fixes https://github.com/clangd/clangd/issues/2275
>From 67b81f57250655c41112b3c3601ca537119df5cc Mon Sep 17 00:00:00 2001
From: Nathan Ridge <zeratul976 at hotmail.com>
Date: Mon, 1 Sep 2025 02:48:44 -0400
Subject: [PATCH] [clangd] Show type hint for simple cases of dependent 'auto'
Fixes https://github.com/clangd/clangd/issues/2275
---
clang-tools-extra/clangd/InlayHints.cpp | 27 ++++++++++++++-----
.../clangd/unittests/InlayHintTests.cpp | 3 ++-
2 files changed, 22 insertions(+), 8 deletions(-)
diff --git a/clang-tools-extra/clangd/InlayHints.cpp b/clang-tools-extra/clangd/InlayHints.cpp
index cd479e1b7c9bc..819e597873d83 100644
--- a/clang-tools-extra/clangd/InlayHints.cpp
+++ b/clang-tools-extra/clangd/InlayHints.cpp
@@ -633,13 +633,26 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
}
if (auto *AT = D->getType()->getContainedAutoType()) {
- if (AT->isDeduced() && !D->getType()->isDependentType()) {
- // Our current approach is to place the hint on the variable
- // and accordingly print the full type
- // (e.g. for `const auto& x = 42`, print `const int&`).
- // Alternatively, we could place the hint on the `auto`
- // (and then just print the type deduced for the `auto`).
- addTypeHint(D->getLocation(), D->getType(), /*Prefix=*/": ");
+ if (AT->isDeduced()) {
+ QualType T;
+ if (D->getType()->isDependentType()) {
+ if (D->hasInit()) {
+ QualType Resolved = Resolver->resolveExprToType(D->getInit());
+ if (Resolved != AST.DependentTy) {
+ T = Resolved;
+ }
+ }
+ } else {
+ T = D->getType();
+ }
+ if (!T.isNull()) {
+ // Our current approach is to place the hint on the variable
+ // and accordingly print the full type
+ // (e.g. for `const auto& x = 42`, print `const int&`).
+ // Alternatively, we could place the hint on the `auto`
+ // (and then just print the type deduced for the `auto`).
+ addTypeHint(D->getLocation(), T, /*Prefix=*/": ");
+ }
}
}
diff --git a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
index 99e728c40063d..feb4404b3d2bf 100644
--- a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
+++ b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
@@ -1441,7 +1441,8 @@ TEST(TypeHints, DependentType) {
void bar(T arg) {
auto [a, b] = arg;
}
- )cpp");
+ )cpp",
+ ExpectedHint{": T", "var2"});
}
TEST(TypeHints, LongTypeName) {
More information about the llvm-branch-commits
mailing list