diff --git a/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp b/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp --- a/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp +++ b/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp @@ -74,6 +74,31 @@ CachedLocation = Result; } } + + if (!CachedLocation) + return false; + + // apply() below will use RecursiveASTVisitor to figure out the deduced + // type. That takes longer, but will handle all cases. + // This scan of parents here only handles one case (RecordDecl) and we only + // do it to prevent offering this tweak on lambda types (common use case for + // 'auto'), since it would fail in apply() anyway. + // + // In the future, We could re-use this node we found here in apply(), but a + // better approach would be to fix getDeducedType() to not require walking + // the entire file and just run in in prepare() instead. + for (auto *It = Node; It; It = It->Parent) { + if (auto *DD = It->ASTNode.get()) { + if (DD->getTypeSourceInfo() && + DD->getTypeSourceInfo()->getTypeLoc().getBeginLoc() == + CachedLocation->getBeginLoc()) { + if (auto *RD = DD->getType()->getAsRecordDecl()) + if (RD->isLambda()) + return false; + break; + } + } + } } return (bool) CachedLocation; } diff --git a/clang-tools-extra/clangd/test/check-fail.test b/clang-tools-extra/clangd/test/check-fail.test --- a/clang-tools-extra/clangd/test/check-fail.test +++ b/clang-tools-extra/clangd/test/check-fail.test @@ -11,4 +11,5 @@ // CHECK: All checks completed, 2 errors #include "missing.h" -auto x = []{}; +void fun(); +auto x = fun; diff --git a/clang-tools-extra/clangd/unittests/TweakTests.cpp b/clang-tools-extra/clangd/unittests/TweakTests.cpp --- a/clang-tools-extra/clangd/unittests/TweakTests.cpp +++ b/clang-tools-extra/clangd/unittests/TweakTests.cpp @@ -559,8 +559,7 @@ EXPECT_THAT(apply("au^to x = &ns::Func;"), StartsWith("fail: Could not expand type of function pointer")); // lambda types are not replaced - EXPECT_THAT(apply("au^to x = []{};"), - StartsWith("fail: Could not expand type of lambda expression")); + EXPECT_UNAVAILABLE("au^to x = []{};"); // inline namespaces EXPECT_EQ(apply("au^to x = inl_ns::Visible();"), "Visible x = inl_ns::Visible();");