18AST_MATCHER(LambdaExpr, hasCoroutineBody) {
19 const Stmt *Body = Node.getBody();
20 return Body !=
nullptr && isa<CoroutineBodyStmt>(Body);
23AST_MATCHER(LambdaExpr, hasCaptures) {
return Node.capture_size() != 0U; }
25AST_MATCHER(LambdaExpr, hasDeducingThis) {
26 return Node.getCallOperator()->isExplicitObjectMemberFunction();
33 AllowExplicitObjectParameters(
34 Options.get(
"AllowExplicitObjectParameters", false)) {}
37 MatchFinder *Finder) {
38 using LambdaExprMatcher = ast_matchers::internal::Matcher<LambdaExpr>;
39 const auto ExplicitObjectFilter =
40 AllowExplicitObjectParameters
41 ? LambdaExprMatcher(unless(hasDeducingThis()))
42 : LambdaExprMatcher(anything());
44 lambdaExpr(hasCaptures(), hasCoroutineBody(), ExplicitObjectFilter)
50 const LangOptions &LangOpts)
const {
51 return LangOpts.CPlusPlus20;
56 Options.store(Opts,
"AllowExplicitObjectParameters",
57 AllowExplicitObjectParameters);
61 const MatchFinder::MatchResult &Result) {
62 const auto *MatchedLambda = Result.Nodes.getNodeAs<LambdaExpr>(
"lambda");
63 diag(MatchedLambda->getExprLoc(),
64 "coroutine lambda may cause use-after-free, avoid captures or ensure "
65 "lambda closure object has guaranteed lifetime");
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.