================
@@ -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:
Please add more tests with different capture syntax from
https://en.cppreference.com/w/cpp/language/lambda.html:
```
The syntax of capture is defined as follows:
identifier (1)
identifier ... (2)
identifier initializer (3) (since C++14)
& identifier (4)
& identifier ... (5)
& identifier initializer (6) (since C++14)
this (7)
* this (8) (since C++17)
... identifier initializer (9) (since C++20)
& ... identifier initializer (10) (since C++20)
```
So it could be a pack expansion lambda (I've never seen any). If it couldn't be
captured by `&` or `=`, make test that it is correctly rewrote when `=` is
present.
https://github.com/llvm/llvm-project/pull/160150
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits