| 1 |
// RUN: %check_clang_tidy -std=c++11-or-later %s cppcoreguidelines-misleading-capture-default-by-value %t |
| 2 |
|
| 3 |
struct Obj { |
| 4 |
void lambdas_that_warn_default_capture_copy() { |
| 5 |
int local{}; |
| 6 |
int local2{}; |
| 7 |
|
| 8 |
auto explicit_this_capture = [=, this]() { }; |
| 9 |
// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: lambdas that capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value] |
| 10 |
// CHECK-FIXES: auto explicit_this_capture = [this]() { }; |
| 11 |
|
| 12 |
auto explicit_this_capture_locals1 = [=, this]() { return (local+x) > 10; }; |
| 13 |
// CHECK-MESSAGES: :[[@LINE-1]]:43: warning: lambdas that capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value] |
| 14 |
// CHECK-FIXES: auto explicit_this_capture_locals1 = [local, this]() { return (local+x) > 10; }; |
| 15 |
|
| 16 |
auto explicit_this_capture_locals2 = [=, this]() { return (local+local2) > 10; }; |
| 17 |
// CHECK-MESSAGES: :[[@LINE-1]]:43: warning: lambdas that capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value] |
| 18 |
// CHECK-FIXES: auto explicit_this_capture_locals2 = [local, local2, this]() { return (local+local2) > 10; }; |
| 19 |
|
| 20 |
auto explicit_this_capture_local_ref = [=, this, &local]() { return (local+x) > 10; }; |
| 21 |
// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: lambdas that capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value] |
| 22 |
// CHECK-FIXES: auto explicit_this_capture_local_ref = [this, &local]() { return (local+x) > 10; }; |
| 23 |
|
| 24 |
auto explicit_this_capture_local_ref2 = [=, &local, this]() { return (local+x) > 10; }; |
| 25 |
// CHECK-MESSAGES: :[[@LINE-1]]:46: warning: lambdas that capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value] |
| 26 |
// CHECK-FIXES: auto explicit_this_capture_local_ref2 = [&local, this]() { return (local+x) > 10; }; |
| 27 |
|
| 28 |
auto explicit_this_capture_local_ref3 = [=, &local, this, &local2]() { return (local+x) > 10; }; |
| 29 |
// CHECK-MESSAGES: :[[@LINE-1]]:46: warning: lambdas that capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value] |
| 30 |
// CHECK-FIXES: auto explicit_this_capture_local_ref3 = [&local, this, &local2]() { return (local+x) > 10; }; |
| 31 |
|
| 32 |
auto explicit_this_capture_local_ref4 = [=, &local, &local2, this]() { return (local+x) > 10; }; |
| 33 |
// CHECK-MESSAGES: :[[@LINE-1]]:46: warning: lambdas that capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value] |
| 34 |
// CHECK-FIXES: auto explicit_this_capture_local_ref4 = [&local, &local2, this]() { return (local+x) > 10; }; |
| 35 |
|
| 36 |
auto explicit_this_capture_local_ref_extra_whitespace = [=, & local, &local2, this]() { return (local+x) > 10; }; |
| 37 |
// CHECK-MESSAGES: :[[@LINE-1]]:62: warning: lambdas that capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value] |
| 38 |
// CHECK-FIXES: auto explicit_this_capture_local_ref_extra_whitespace = [& local, &local2, this]() { return (local+x) > 10; }; |
| 39 |
|
| 40 |
auto explicit_this_capture_local_ref_with_comment = [=, & /* byref */ local, &local2, this]() { return (local+x) > 10; }; |
| 41 |
// CHECK-MESSAGES: :[[@LINE-1]]:58: warning: lambdas that capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value] |
| 42 |
// CHECK-FIXES: auto explicit_this_capture_local_ref_with_comment = [& /* byref */ local, &local2, this]() { return (local+x) > 10; }; |
| 43 |
|
| 44 |
auto implicit_this_capture = [=]() { return x > 10; }; |
| 45 |
// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: lambdas that implicitly capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value] |
| 46 |
// CHECK-FIXES: auto implicit_this_capture = [this]() { return x > 10; }; |
| 47 |
|
| 48 |
auto implicit_this_capture_local = [=]() { return (local+x) > 10; }; |
| 49 |
// CHECK-MESSAGES: :[[@LINE-1]]:41: warning: lambdas that implicitly capture 'this' should not specify a by-value capture default [cppcoreguidelines-misleading-capture-default-by-value] |
| 50 |
// CHECK-FIXES: auto implicit_this_capture_local = [local, this]() { return (local+x) > 10; }; |
| 51 |
} |
| 52 |
|
| 53 |
void lambdas_that_dont_warn_default_capture_ref() { |
| 54 |
int local{}; |
| 55 |
int local2{}; |
| 56 |
|
| 57 |
auto ref_explicit_this_capture = [&, this]() { }; |
| 58 |
auto ref_explicit_this_capture_local = [&, this]() { return (local+x) > 10; }; |
| 59 |
|
| 60 |
auto ref_implicit_this_capture = [&]() { return x > 10; }; |
| 61 |
auto ref_implicit_this_capture_local = [&]() { return (local+x) > 10; }; |
| 62 |
auto ref_implicit_this_capture_locals = [&]() { return (local+local2+x) > 10; }; |
| 63 |
} |
| 64 |
|
| 65 |
void lambdas_that_dont_warn() { |
| 66 |
int local{}; |
| 67 |
int local2{}; |
| 68 |
auto explicit_this_capture = [this]() { }; |
| 69 |
auto explicit_this_capture_local = [this, local]() { return local > 10; }; |
| 70 |
auto explicit_this_capture_local_ref = [this, &local]() { return local > 10; }; |
| 71 |
|
| 72 |
auto no_captures = []() {}; |
| 73 |
auto capture_local_only = [local]() {}; |
| 74 |
auto ref_capture_local_only = [&local]() {}; |
| 75 |
auto capture_many = [local, &local2]() {}; |
| 76 |
} |
| 77 |
|
| 78 |
int x; |
| 79 |
}; |