diff --git a/clang-tools-extra/clangd/ExpectedTypes.cpp b/clang-tools-extra/clangd/ExpectedTypes.cpp index 3b0779ea66bc6d..a82a64cf14e2bb 100644 --- a/clang-tools-extra/clangd/ExpectedTypes.cpp +++ b/clang-tools-extra/clangd/ExpectedTypes.cpp @@ -44,12 +44,10 @@ static const Type *toEquivClass(ASTContext &Ctx, QualType T) { static llvm::Optional typeOfCompletion(const CodeCompletionResult &R) { const NamedDecl *D = R.Declaration; - if (!D) - return llvm::None; // Templates do not have a type on their own, look at the templated decl. - if (auto *Template = dyn_cast(D)) + if (auto *Template = dyn_cast_or_null(D)) D = Template->getTemplatedDecl(); - auto *VD = dyn_cast(D); + auto *VD = dyn_cast_or_null(D); if (!VD) return llvm::None; // We handle only variables and functions below. auto T = VD->getType(); diff --git a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp index 5b50b9fe9f8ba5..e69b2a6205f6a8 100644 --- a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp +++ b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp @@ -1030,6 +1030,16 @@ TEST(CompletionTest, DefaultArgs) { SnippetSuffix("(${1:int A})")))); } +TEST(CompletionTest, NoCrashWithTemplateParamsAndPreferredTypes) { + auto Completions = completions(R"cpp( +template