307 lines · cpp
1// RUN: %check_clang_tidy %s performance-noexcept-destructor %t -- -- -fexceptions2 3struct Empty4{};5 6struct IntWrapper {7 int value;8};9 10template <typename T>11struct FalseT {12 static constexpr bool value = false;13};14 15template <typename T>16struct TrueT {17 static constexpr bool value = true;18};19 20struct ThrowOnAnything {21 ThrowOnAnything() noexcept(false);22 ThrowOnAnything(ThrowOnAnything&&) noexcept(false);23 ThrowOnAnything& operator=(ThrowOnAnything &&) noexcept(false);24 ~ThrowOnAnything() noexcept(false);25};26 27struct B {28 static constexpr bool kFalse = false;29 ~B() noexcept(kFalse);30 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: noexcept specifier on the destructor evaluates to 'false' [performance-noexcept-destructor]31};32 33struct D {34 static constexpr bool kFalse = false;35 ~D() noexcept(kFalse) = default;36 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: noexcept specifier on the destructor evaluates to 'false' [performance-noexcept-destructor]37};38 39template <typename>40struct E {41 static constexpr bool kFalse = false;42 ~E() noexcept(kFalse);43 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: noexcept specifier on the destructor evaluates to 'false'44};45 46template <typename>47struct F {48 static constexpr bool kFalse = false;49 ~F() noexcept(kFalse) = default;50 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: noexcept specifier on the destructor evaluates to 'false' [performance-noexcept-destructor]51};52 53struct G {54 ~G() = default;55 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: destructors should be marked noexcept [performance-noexcept-destructor]56 // CHECK-FIXES: ~G() noexcept = default;57 58 ThrowOnAnything field;59};60 61void throwing_function() noexcept(false) {}62 63struct H {64 ~H() noexcept(noexcept(throwing_function()));65 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: noexcept specifier on the destructor evaluates to 'false' [performance-noexcept-destructor]66};67 68template <typename>69struct I {70 ~I() noexcept(noexcept(throwing_function()));71 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: noexcept specifier on the destructor evaluates to 'false' [performance-noexcept-destructor]72};73 74template <typename T> struct TemplatedType {75 static void f() {}76};77 78template <> struct TemplatedType<int> {79 static void f() noexcept {}80};81 82struct J {83 ~J() noexcept(noexcept(TemplatedType<double>::f()));84 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: noexcept specifier on the destructor evaluates to 'false' [performance-noexcept-destructor]85};86 87struct K : public ThrowOnAnything {88 ~K() = default;89 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: destructors should be marked noexcept [performance-noexcept-destructor]90 // CHECK-FIXES: ~K() noexcept = default;91};92 93struct InheritFromThrowOnAnything : public ThrowOnAnything94{};95 96struct L {97 ~L() = default;98 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: destructors should be marked noexcept [performance-noexcept-destructor]99 // CHECK-FIXES: ~L() noexcept = default;100 101 InheritFromThrowOnAnything IFF;102};103 104struct M : public InheritFromThrowOnAnything {105 ~M() = default;106 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: destructors should be marked noexcept [performance-noexcept-destructor]107 // CHECK-FIXES: ~M() noexcept = default;108};109 110struct N : public IntWrapper, ThrowOnAnything {111 ~N() = default;112 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: destructors should be marked noexcept [performance-noexcept-destructor]113 // CHECK-FIXES: ~N() noexcept = default;114};115 116struct O : virtual IntWrapper, ThrowOnAnything {117 ~O() = default;118 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: destructors should be marked noexcept [performance-noexcept-destructor]119 // CHECK-FIXES: ~O() noexcept = default;120};121 122class OK {};123 124struct OK1 {125 ~OK1() noexcept;126};127 128struct OK2 {129 static constexpr bool kTrue = true;130 131 ~OK2() noexcept(true) {}132};133 134struct OK4 {135 ~OK4() noexcept(false) {}136};137 138struct OK3 {139 ~OK3() = default;140};141 142struct OK5 {143 ~OK5() noexcept(true) = default;144};145 146struct OK6 {147 ~OK6() = default;148};149 150template <typename>151struct OK7 {152 ~OK7() = default;153};154 155template <typename>156struct OK8 {157 ~OK8() noexcept = default;158};159 160template <typename>161struct OK9 {162 ~OK9() noexcept(true) = default;163};164 165template <typename>166struct OK10 {167 ~OK10() noexcept(false) = default;168};169 170template <typename>171struct OK11 {172 ~OK11() = delete;173};174 175void noexcept_function() noexcept {}176 177struct OK12 {178 ~OK12() noexcept(noexcept(noexcept_function()));179};180 181struct OK13 {182 ~OK13() noexcept(noexcept(noexcept_function())) = default;183};184 185template <typename>186struct OK14 {187 ~OK14() noexcept(noexcept(TemplatedType<int>::f()));188};189 190struct OK15 {191 ~OK15() = default;192 193 int member;194};195 196template <typename>197struct OK16 {198 ~OK16() = default;199 200 int member;201};202 203struct OK17 {204 ~OK17() = default;205 206 OK empty_field;207};208 209template <typename>210struct OK18 {211 ~OK18() = default;212 213 OK empty_field;214};215 216struct OK19 : public OK {217 ~OK19() = default;218};219 220struct OK20 : virtual OK {221 ~OK20() = default;222};223 224template <typename T>225struct OK21 : public T {226 ~OK21() = default;227};228 229template <typename T>230struct OK22 : virtual T {231 ~OK22() = default;232};233 234template <typename T>235struct OK23 {236 ~OK23() = default;237 238 T member;239};240 241void testTemplates() {242 OK21<Empty> value(OK21<Empty>{});243 value = OK21<Empty>{};244 245 OK22<Empty> value2{OK22<Empty>{}};246 value2 = OK22<Empty>{};247 248 OK23<Empty> value3{OK23<Empty>{}};249 value3 =OK23<Empty>{};250}251 252struct OK24 : public Empty, OK1 {253 ~OK24() = default;254};255 256struct OK25 : virtual Empty, OK1 {257 ~OK25() = default;258};259 260struct OK26 : public Empty, IntWrapper {261 ~OK26() = default;262};263 264template <typename T>265struct OK27 : public T {266 ~OK27() = default;267};268 269template <typename T>270struct OK28 : virtual T {271 ~OK28() = default;272};273 274template <typename T>275struct OK29 {276 ~OK29() = default;277 278 T member;279};280 281struct OK30 {282 ~OK30() noexcept(TrueT<OK30>::value) = default;283};284 285template <typename>286struct OK31 {287 ~OK31() noexcept(TrueT<int>::value) = default;288};289 290struct OK32 {291 ~OK32();292};293 294template <typename>295struct OK33 {296 ~OK33();297};298 299struct OK34 {300 ~OK34() {}301};302 303template <typename>304struct OK35 {305 ~OK35() {}306};307