1*eedbe81bSCarlos Galvez // RUN: %check_clang_tidy -std=c++11-or-later %s cppcoreguidelines-misleading-capture-default-by-value %t
2*eedbe81bSCarlos Galvez 
3*eedbe81bSCarlos Galvez struct Obj {
lambdas_that_warn_default_capture_copyObj4*eedbe81bSCarlos Galvez   void lambdas_that_warn_default_capture_copy() {
5*eedbe81bSCarlos Galvez     int local{};
6*eedbe81bSCarlos Galvez     int local2{};
7*eedbe81bSCarlos Galvez 
8*eedbe81bSCarlos Galvez     auto explicit_this_capture = [=, this]() { };
9*eedbe81bSCarlos Galvez     // 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*eedbe81bSCarlos Galvez     // CHECK-FIXES: auto explicit_this_capture = [this]() { };
11*eedbe81bSCarlos Galvez 
12*eedbe81bSCarlos Galvez     auto explicit_this_capture_locals1 = [=, this]() { return (local+x) > 10; };
13*eedbe81bSCarlos Galvez     // 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*eedbe81bSCarlos Galvez     // CHECK-FIXES: auto explicit_this_capture_locals1 = [local, this]() { return (local+x) > 10; };
15*eedbe81bSCarlos Galvez 
16*eedbe81bSCarlos Galvez     auto explicit_this_capture_locals2 = [=, this]() { return (local+local2) > 10; };
17*eedbe81bSCarlos Galvez     // 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*eedbe81bSCarlos Galvez     // CHECK-FIXES: auto explicit_this_capture_locals2 = [local, local2, this]() { return (local+local2) > 10; };
19*eedbe81bSCarlos Galvez 
20*eedbe81bSCarlos Galvez     auto explicit_this_capture_local_ref = [=, this, &local]() { return (local+x) > 10; };
21*eedbe81bSCarlos Galvez     // 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*eedbe81bSCarlos Galvez     // CHECK-FIXES: auto explicit_this_capture_local_ref = [this, &local]() { return (local+x) > 10; };
23*eedbe81bSCarlos Galvez 
24*eedbe81bSCarlos Galvez     auto explicit_this_capture_local_ref2 = [=, &local, this]() { return (local+x) > 10; };
25*eedbe81bSCarlos Galvez     // 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*eedbe81bSCarlos Galvez     // CHECK-FIXES: auto explicit_this_capture_local_ref2 = [&local, this]() { return (local+x) > 10; };
27*eedbe81bSCarlos Galvez 
28*eedbe81bSCarlos Galvez     auto explicit_this_capture_local_ref3 = [=, &local, this, &local2]() { return (local+x) > 10; };
29*eedbe81bSCarlos Galvez     // 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*eedbe81bSCarlos Galvez     // CHECK-FIXES: auto explicit_this_capture_local_ref3 = [&local, this, &local2]() { return (local+x) > 10; };
31*eedbe81bSCarlos Galvez 
32*eedbe81bSCarlos Galvez     auto explicit_this_capture_local_ref4 = [=, &local, &local2, this]() { return (local+x) > 10; };
33*eedbe81bSCarlos Galvez     // 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*eedbe81bSCarlos Galvez     // CHECK-FIXES: auto explicit_this_capture_local_ref4 = [&local, &local2, this]() { return (local+x) > 10; };
35*eedbe81bSCarlos Galvez 
36*eedbe81bSCarlos Galvez     auto explicit_this_capture_local_ref_extra_whitespace = [=, &  local, &local2, this]() { return (local+x) > 10; };
37*eedbe81bSCarlos Galvez     // 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*eedbe81bSCarlos Galvez     // CHECK-FIXES: auto explicit_this_capture_local_ref_extra_whitespace = [&  local, &local2, this]() { return (local+x) > 10; };
39*eedbe81bSCarlos Galvez 
40*eedbe81bSCarlos Galvez     auto explicit_this_capture_local_ref_with_comment = [=, & /* byref */ local, &local2, this]() { return (local+x) > 10; };
41*eedbe81bSCarlos Galvez     // 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*eedbe81bSCarlos Galvez     // CHECK-FIXES: auto explicit_this_capture_local_ref_with_comment = [& /* byref */ local, &local2, this]() { return (local+x) > 10; };
43*eedbe81bSCarlos Galvez 
44*eedbe81bSCarlos Galvez     auto implicit_this_capture = [=]() { return x > 10; };
45*eedbe81bSCarlos Galvez     // 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*eedbe81bSCarlos Galvez     // CHECK-FIXES: auto implicit_this_capture = [this]() { return x > 10; };
47*eedbe81bSCarlos Galvez 
48*eedbe81bSCarlos Galvez     auto implicit_this_capture_local = [=]() { return (local+x) > 10; };
49*eedbe81bSCarlos Galvez     // 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*eedbe81bSCarlos Galvez     // CHECK-FIXES: auto implicit_this_capture_local = [local, this]() { return (local+x) > 10; };
51*eedbe81bSCarlos Galvez   }
52*eedbe81bSCarlos Galvez 
lambdas_that_dont_warn_default_capture_refObj53*eedbe81bSCarlos Galvez   void lambdas_that_dont_warn_default_capture_ref() {
54*eedbe81bSCarlos Galvez     int local{};
55*eedbe81bSCarlos Galvez     int local2{};
56*eedbe81bSCarlos Galvez 
57*eedbe81bSCarlos Galvez     auto ref_explicit_this_capture = [&, this]() { };
58*eedbe81bSCarlos Galvez     auto ref_explicit_this_capture_local = [&, this]() { return (local+x) > 10; };
59*eedbe81bSCarlos Galvez 
60*eedbe81bSCarlos Galvez     auto ref_implicit_this_capture = [&]() { return x > 10; };
61*eedbe81bSCarlos Galvez     auto ref_implicit_this_capture_local = [&]() { return (local+x) > 10; };
62*eedbe81bSCarlos Galvez     auto ref_implicit_this_capture_locals = [&]() { return (local+local2+x) > 10; };
63*eedbe81bSCarlos Galvez   }
64*eedbe81bSCarlos Galvez 
lambdas_that_dont_warnObj65*eedbe81bSCarlos Galvez   void lambdas_that_dont_warn() {
66*eedbe81bSCarlos Galvez     int local{};
67*eedbe81bSCarlos Galvez     int local2{};
68*eedbe81bSCarlos Galvez     auto explicit_this_capture = [this]() { };
69*eedbe81bSCarlos Galvez     auto explicit_this_capture_local = [this, local]() { return local > 10; };
70*eedbe81bSCarlos Galvez     auto explicit_this_capture_local_ref = [this, &local]() { return local > 10; };
71*eedbe81bSCarlos Galvez 
72*eedbe81bSCarlos Galvez     auto no_captures = []() {};
73*eedbe81bSCarlos Galvez     auto capture_local_only = [local]() {};
74*eedbe81bSCarlos Galvez     auto ref_capture_local_only = [&local]() {};
75*eedbe81bSCarlos Galvez     auto capture_many = [local, &local2]() {};
76*eedbe81bSCarlos Galvez   }
77*eedbe81bSCarlos Galvez 
78*eedbe81bSCarlos Galvez   int x;
79*eedbe81bSCarlos Galvez };
80