[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 Nov 20 23:51:51 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:

Please add unit tests in clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

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


More information about the cfe-commits mailing list