diff --git a/clang/lib/ASTMatchers/ASTMatchFinder.cpp b/clang/lib/ASTMatchers/ASTMatchFinder.cpp --- a/clang/lib/ASTMatchers/ASTMatchFinder.cpp +++ b/clang/lib/ASTMatchers/ASTMatchFinder.cpp @@ -295,7 +295,7 @@ if (!match(*Node->getBody())) return false; - return true; + return VisitorBase::TraverseStmt(Node->getBody()); } bool shouldVisitTemplateInstantiations() const { return true; } diff --git a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp --- a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp +++ b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp @@ -3220,6 +3220,12 @@ float i = 42.0; } +void func15() { + int count = 0; + auto l = [&] { ++count; }; + (void)l; +} + )cpp"; EXPECT_TRUE( @@ -3404,6 +3410,15 @@ functionDecl(hasName("func14"), hasDescendant(floatLiteral()))), langCxx20OrLater())); + EXPECT_TRUE(matches( + Code, + traverse(TK_IgnoreUnlessSpelledInSource, + compoundStmt( + hasDescendant(varDecl(hasName("count")).bind("countVar")), + hasDescendant( + declRefExpr(to(varDecl(equalsBoundNode("countVar"))))))), + langCxx20OrLater())); + Code = R"cpp( void foo() { int explicit_captured = 0;