[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
Fri Dec 12 03:03:46 PST 2025


================
@@ -5125,6 +5125,20 @@ AST_MATCHER_P(LambdaCapture, capturesVar, internal::Matcher<ValueDecl>,
 ///   matches `[this]() { return cc; }`.
 AST_MATCHER(LambdaCapture, capturesThis) { return Node.capturesThis(); }
 
+/// Matches lambda expressions that have default capture modes.
+///
+/// Given
+/// \code
+///   auto l1 = [=]() {};  // matches
+///   auto l2 = [&]() {};  // matches
+///   auto l3 = []() {};   // does not match
+/// \endcode
+/// lambdaExpr(hasDefaultCapture())
+///   matches l1 and l2, but not l3.
+AST_MATCHER(LambdaExpr, hasDefaultCapture) {
+  return Node.getCaptureDefault() != LCD_None;
+}
+
----------------
vbvictor wrote:

Could we move this into check code (AvoidDefaultLambdaCheck.cpp) or add this to ast-matchers as separate PR. (also it needs tests)

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


More information about the cfe-commits mailing list