[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
Wed Oct 15 13:16:50 PDT 2025
================
@@ -0,0 +1,143 @@
+// RUN: %check_clang_tidy %s readability-avoid-default-lambda-capture %t -- -- -Wno-vla-extension
+
+void test_default_captures() {
+ int value = 42;
+ int another = 10;
+
+ auto lambda1 = [=](int x) { return value + x; };
+ // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: lambda default captures are discouraged; prefer to capture specific variables explicitly [readability-avoid-default-lambda-capture]
+ // CHECK-FIXES: auto lambda1 = [value](int x) { return value + x; };
+
+ auto lambda2 = [&](int x) { return value + x; };
+ // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: lambda default captures are discouraged; prefer to capture specific variables explicitly [readability-avoid-default-lambda-capture]
+ // CHECK-FIXES: auto lambda2 = [&value](int x) { return value + x; };
+
+ auto lambda3 = [=, &another](int x) { return value + another + x; };
+ // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: lambda default captures are discouraged; prefer to capture specific variables explicitly [readability-avoid-default-lambda-capture]
+ // CHECK-FIXES: auto lambda3 = [value, &another](int x) { return value + another + x; };
+
+ auto lambda4 = [&, value](int x) { return value + another + x; };
+ // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: lambda default captures are discouraged; prefer to capture specific variables explicitly [readability-avoid-default-lambda-capture]
+ // CHECK-FIXES: auto lambda4 = [&another, value](int x) { return value + another + x; };
+}
+
+void test_acceptable_captures() {
----------------
vbvictor wrote:
Sure, I don't mind leaving it without a fix
https://github.com/llvm/llvm-project/pull/160150
More information about the cfe-commits
mailing list