|
| 1 | +// RUN: %check_clang_tidy -std=c++11-or-later %s bugprone-capture-this-by-field %t |
| 2 | + |
| 3 | +namespace std { |
| 4 | + |
| 5 | +template<class Fn> |
| 6 | +class function; |
| 7 | + |
| 8 | +template<class R, class ...Args> |
| 9 | +class function<R(Args...)> { |
| 10 | +public: |
| 11 | + function() noexcept; |
| 12 | + template<class F> function(F &&); |
| 13 | +}; |
| 14 | + |
| 15 | +} // namespace std |
| 16 | + |
| 17 | +struct Basic { |
| 18 | + Basic() : Captured([this]() { static_cast<void>(this); }) {} |
| 19 | + // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: using lambda expressions to capture this and storing it in class member |
| 20 | + std::function<void()> Captured; |
| 21 | + // CHECK-MESSAGES: :[[@LINE-1]]:25: note: 'std::function' that stores captured this |
| 22 | +}; |
| 23 | + |
| 24 | +struct AssignCapture { |
| 25 | + AssignCapture() : Captured([Self = this]() { static_cast<void>(Self); }) {} |
| 26 | + // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: using lambda expressions to capture this and storing it in class member |
| 27 | + std::function<void()> Captured; |
| 28 | + // CHECK-MESSAGES: :[[@LINE-1]]:25: note: 'std::function' that stores captured this |
| 29 | +}; |
| 30 | + |
| 31 | +struct DeleteMoveAndCopy { |
| 32 | + DeleteMoveAndCopy() : Captured([this]() { static_cast<void>(this); }) {} |
| 33 | + DeleteMoveAndCopy(DeleteMoveAndCopy const&) = delete; |
| 34 | + DeleteMoveAndCopy(DeleteMoveAndCopy &&) = delete; |
| 35 | + DeleteMoveAndCopy& operator=(DeleteMoveAndCopy const&) = delete; |
| 36 | + DeleteMoveAndCopy& operator=(DeleteMoveAndCopy &&) = delete; |
| 37 | + std::function<void()> Captured; |
| 38 | +}; |
| 39 | + |
| 40 | +struct DeleteCopyImplicitDisabledMove { |
| 41 | + DeleteCopyImplicitDisabledMove() : Captured([this]() { static_cast<void>(this); }) {} |
| 42 | + DeleteCopyImplicitDisabledMove(DeleteCopyImplicitDisabledMove const&) = delete; |
| 43 | + DeleteCopyImplicitDisabledMove& operator=(DeleteCopyImplicitDisabledMove const&) = delete; |
| 44 | + std::function<void()> Captured; |
| 45 | +}; |
| 46 | + |
| 47 | +struct DeleteCopyDefaultMove { |
| 48 | + DeleteCopyDefaultMove() : Captured([this]() { static_cast<void>(this); }) {} |
| 49 | + // CHECK-MESSAGES: :[[@LINE-1]]:38: warning: using lambda expressions to capture this and storing it in class member |
| 50 | + DeleteCopyDefaultMove(DeleteCopyDefaultMove const&) = delete; |
| 51 | + DeleteCopyDefaultMove(DeleteCopyDefaultMove &&) = default; |
| 52 | + DeleteCopyDefaultMove& operator=(DeleteCopyDefaultMove const&) = delete; |
| 53 | + DeleteCopyDefaultMove& operator=(DeleteCopyDefaultMove &&) = default; |
| 54 | + std::function<void()> Captured; |
| 55 | + // CHECK-MESSAGES: :[[@LINE-1]]:25: note: 'std::function' that stores captured this |
| 56 | +}; |
| 57 | + |
| 58 | +struct DeleteMoveDefaultCopy { |
| 59 | + DeleteMoveDefaultCopy() : Captured([this]() { static_cast<void>(this); }) {} |
| 60 | + // CHECK-MESSAGES: :[[@LINE-1]]:38: warning: using lambda expressions to capture this and storing it in class member |
| 61 | + DeleteMoveDefaultCopy(DeleteMoveDefaultCopy const&) = default; |
| 62 | + DeleteMoveDefaultCopy(DeleteMoveDefaultCopy &&) = delete; |
| 63 | + DeleteMoveDefaultCopy& operator=(DeleteMoveDefaultCopy const&) = default; |
| 64 | + DeleteMoveDefaultCopy& operator=(DeleteMoveDefaultCopy &&) = delete; |
| 65 | + std::function<void()> Captured; |
| 66 | + // CHECK-MESSAGES: :[[@LINE-1]]:25: note: 'std::function' that stores captured this |
| 67 | +}; |
| 68 | + |
| 69 | +struct DeleteCopyMoveBase { |
| 70 | + DeleteCopyMoveBase() = default; |
| 71 | + DeleteCopyMoveBase(DeleteCopyMoveBase const&) = delete; |
| 72 | + DeleteCopyMoveBase(DeleteCopyMoveBase &&) = delete; |
| 73 | + DeleteCopyMoveBase& operator=(DeleteCopyMoveBase const&) = delete; |
| 74 | + DeleteCopyMoveBase& operator=(DeleteCopyMoveBase &&) = delete; |
| 75 | +}; |
| 76 | + |
| 77 | +struct Inherit : DeleteCopyMoveBase { |
| 78 | + Inherit() : DeleteCopyMoveBase{}, Captured([this]() { static_cast<void>(this); }) {} |
| 79 | + std::function<void()> Captured; |
| 80 | +}; |
| 81 | + |
| 82 | +struct UserDefinedCopyMove { |
| 83 | + UserDefinedCopyMove() : Captured([this]() { static_cast<void>(this); }) {} |
| 84 | + UserDefinedCopyMove(UserDefinedCopyMove const&); |
| 85 | + UserDefinedCopyMove(UserDefinedCopyMove &&); |
| 86 | + UserDefinedCopyMove& operator=(UserDefinedCopyMove const&); |
| 87 | + UserDefinedCopyMove& operator=(UserDefinedCopyMove &&); |
| 88 | + std::function<void()> Captured; |
| 89 | +}; |
| 90 | + |
| 91 | +struct UserDefinedCopyMoveWithDefault1 { |
| 92 | + UserDefinedCopyMoveWithDefault1() : Captured([this]() { static_cast<void>(this); }) {} |
| 93 | + // CHECK-MESSAGES: :[[@LINE-1]]:48: warning: using lambda expressions to capture this and storing it in class member |
| 94 | + UserDefinedCopyMoveWithDefault1(UserDefinedCopyMoveWithDefault1 const&) = default; |
| 95 | + UserDefinedCopyMoveWithDefault1(UserDefinedCopyMoveWithDefault1 &&); |
| 96 | + UserDefinedCopyMoveWithDefault1& operator=(UserDefinedCopyMoveWithDefault1 const&); |
| 97 | + UserDefinedCopyMoveWithDefault1& operator=(UserDefinedCopyMoveWithDefault1 &&); |
| 98 | + std::function<void()> Captured; |
| 99 | + // CHECK-MESSAGES: :[[@LINE-1]]:25: note: 'std::function' that stores captured this |
| 100 | +}; |
| 101 | + |
| 102 | +struct UserDefinedCopyMoveWithDefault2 { |
| 103 | + UserDefinedCopyMoveWithDefault2() : Captured([this]() { static_cast<void>(this); }) {} |
| 104 | + // CHECK-MESSAGES: :[[@LINE-1]]:48: warning: using lambda expressions to capture this and storing it in class member |
| 105 | + UserDefinedCopyMoveWithDefault2(UserDefinedCopyMoveWithDefault2 const&); |
| 106 | + UserDefinedCopyMoveWithDefault2(UserDefinedCopyMoveWithDefault2 &&) = default; |
| 107 | + UserDefinedCopyMoveWithDefault2& operator=(UserDefinedCopyMoveWithDefault2 const&); |
| 108 | + UserDefinedCopyMoveWithDefault2& operator=(UserDefinedCopyMoveWithDefault2 &&); |
| 109 | + std::function<void()> Captured; |
| 110 | + // CHECK-MESSAGES: :[[@LINE-1]]:25: note: 'std::function' that stores captured this |
| 111 | +}; |
| 112 | + |
| 113 | +struct UserDefinedCopyMoveWithDefault3 { |
| 114 | + UserDefinedCopyMoveWithDefault3() : Captured([this]() { static_cast<void>(this); }) {} |
| 115 | + // CHECK-MESSAGES: :[[@LINE-1]]:48: warning: using lambda expressions to capture this and storing it in class member |
| 116 | + UserDefinedCopyMoveWithDefault3(UserDefinedCopyMoveWithDefault3 const&); |
| 117 | + UserDefinedCopyMoveWithDefault3(UserDefinedCopyMoveWithDefault3 &&); |
| 118 | + UserDefinedCopyMoveWithDefault3& operator=(UserDefinedCopyMoveWithDefault3 const&) = default; |
| 119 | + UserDefinedCopyMoveWithDefault3& operator=(UserDefinedCopyMoveWithDefault3 &&); |
| 120 | + std::function<void()> Captured; |
| 121 | + // CHECK-MESSAGES: :[[@LINE-1]]:25: note: 'std::function' that stores captured this |
| 122 | +}; |
| 123 | + |
| 124 | +struct UserDefinedCopyMoveWithDefault4 { |
| 125 | + UserDefinedCopyMoveWithDefault4() : Captured([this]() { static_cast<void>(this); }) {} |
| 126 | + // CHECK-MESSAGES: :[[@LINE-1]]:48: warning: using lambda expressions to capture this and storing it in class member |
| 127 | + UserDefinedCopyMoveWithDefault4(UserDefinedCopyMoveWithDefault4 const&); |
| 128 | + UserDefinedCopyMoveWithDefault4(UserDefinedCopyMoveWithDefault4 &&); |
| 129 | + UserDefinedCopyMoveWithDefault4& operator=(UserDefinedCopyMoveWithDefault4 const&); |
| 130 | + UserDefinedCopyMoveWithDefault4& operator=(UserDefinedCopyMoveWithDefault4 &&) = default; |
| 131 | + std::function<void()> Captured; |
| 132 | + // CHECK-MESSAGES: :[[@LINE-1]]:25: note: 'std::function' that stores captured this |
| 133 | +}; |
0 commit comments