[clang] [clang-tools-extra] [clang-tidy] Add readability-avoid-default-lambda-capture (PR #160150)

Baranov Victor via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 9 11:55:30 PDT 2025


================
@@ -0,0 +1,71 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "AvoidDefaultLambdaCaptureCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Basic/Lambda.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::tidy::readability;
+
+static std::string generateCaptureText(const clang::LambdaCapture &Capture) {
+  if (Capture.capturesThis()) {
+    return Capture.getCaptureKind() == clang::LCK_StarThis ? "*this" : "this";
+  }
+
+  std::string Result;
+  if (Capture.getCaptureKind() == clang::LCK_ByRef) {
+    Result += "&";
+  }
+  Result += Capture.getCapturedVar()->getName().str();
+  return Result;
+}
+
+void AvoidDefaultLambdaCaptureCheck::registerMatchers(
+    clang::ast_matchers::MatchFinder *Finder) {
+  Finder->addMatcher(
+      clang::ast_matchers::lambdaExpr(clang::ast_matchers::hasDefaultCapture())
+          .bind("lambda"),
+      this);
+}
+
+void AvoidDefaultLambdaCaptureCheck::check(
+    const clang::ast_matchers::MatchFinder::MatchResult &Result) {
+  const auto *Lambda = Result.Nodes.getNodeAs<clang::LambdaExpr>("lambda");
+  assert(Lambda);
+
+  const clang::SourceLocation DefaultCaptureLoc =
----------------
vbvictor wrote:

After https://github.com/llvm/llvm-project/pull/160150/files#r2415405342
Should be able to omit all `clang::`
```suggestion
  const SourceLocation DefaultCaptureLoc =
```

https://github.com/llvm/llvm-project/pull/160150


More information about the cfe-commits mailing list