diff --git a/clang-tools-extra/clangd/XRefs.cpp b/clang-tools-extra/clangd/XRefs.cpp index b0ed6143a114..33369608dcf1 100644 --- a/clang-tools-extra/clangd/XRefs.cpp +++ b/clang-tools-extra/clangd/XRefs.cpp @@ -605,6 +605,7 @@ public: // Handle auto return types: //- auto foo() {} //- auto& foo() {} + //- auto foo() -> int {} //- auto foo() -> decltype(1+1) {} //- operator auto() const { return 10; } bool VisitFunctionDecl(FunctionDecl *D) { @@ -624,12 +625,13 @@ public: const AutoType *AT = D->getReturnType()->getContainedAutoType(); if (AT && !AT->getDeducedType().isNull()) { DeducedType = AT->getDeducedType(); - } else { + } else if (auto DT = dyn_cast(D->getReturnType())) { // auto in a trailing return type just points to a DecltypeType and // getContainedAutoType does not unwrap it. - const DecltypeType *DT = dyn_cast(D->getReturnType()); if (!DT->getUnderlyingType().isNull()) DeducedType = DT->getUnderlyingType(); + } else if (!D->getReturnType().isNull()) { + DeducedType = D->getReturnType(); } return true; } diff --git a/clang-tools-extra/unittests/clangd/XRefsTests.cpp b/clang-tools-extra/unittests/clangd/XRefsTests.cpp index 74c0d9e5e015..0cc87bb9cd77 100644 --- a/clang-tools-extra/unittests/clangd/XRefsTests.cpp +++ b/clang-tools-extra/unittests/clangd/XRefsTests.cpp @@ -835,6 +835,14 @@ TEST(Hover, All) { )cpp", "", }, + { + R"cpp(// simple trailing return type + ^auto main() -> int { + return 0; + } + )cpp", + "int", + }, { R"cpp(// auto function return with trailing type struct Bar {};