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

JJ Marr via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 29 10:30:08 PDT 2025


================
@@ -0,0 +1,94 @@
+//===----------------------------------------------------------------------===//
+//
+// 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::ast_matchers;
+
+namespace clang::tidy::readability {
+
+namespace {
+AST_MATCHER(LambdaExpr, hasDefaultCapture) {
+  return Node.getCaptureDefault() != LCD_None;
+}
+
+std::optional<std::string>
+generateImplicitCaptureText(const LambdaCapture &Capture) {
+  if (Capture.capturesThis()) {
+    return Capture.getCaptureKind() == LCK_StarThis ? "*this" : "this";
+  }
+
+  if (Capture.capturesVariable()) {
+    std::string Result;
+    if (Capture.getCaptureKind() == LCK_ByRef) {
+      Result += "&";
+    }
+    Result += Capture.getCapturedVar()->getName().str();
+    return Result;
+  }
+
+  if (Capture.capturesVLAType()) {
+    // VLA captures are rare and complex - for now we skip them
+    // A full implementation would need to handle the VLA type properly
+    return std::nullopt;
+  }
----------------
jjmarr-amd wrote:

I have no idea how I'm supposed to handle this. Also, I think I need to add a TODO.

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


More information about the cfe-commits mailing list