diff options
author | Aaron Ballman <aaron@aaronballman.com> | 2018-11-28 15:04:38 +0000 |
---|---|---|
committer | Aaron Ballman <aaron@aaronballman.com> | 2018-11-28 15:04:38 +0000 |
commit | 5ae4ac2fed7d39213e6a0029500310cc4e7f1689 (patch) | |
tree | fab2aaf58f4d4b790de0e1d6a8fe9f7d9a1495d8 /clang-tools-extra/test | |
parent | aded6828c379bb129cadeb1fdd04097cefe4e29b (diff) |
Fix false positive with lambda assignments in cert-err58-cpp.
This check is about preventing exceptions from being thrown before main() executes, and assigning a lambda (rather than calling it) to a global object cannot throw any exceptions.
Diffstat (limited to 'clang-tools-extra/test')
-rw-r--r-- | clang-tools-extra/test/clang-tidy/cert-static-object-exception.cpp | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/clang-tools-extra/test/clang-tidy/cert-static-object-exception.cpp b/clang-tools-extra/test/clang-tidy/cert-static-object-exception.cpp index 9181dc7ebaa..5c07c8d606a 100644 --- a/clang-tools-extra/test/clang-tidy/cert-static-object-exception.cpp +++ b/clang-tools-extra/test/clang-tidy/cert-static-object-exception.cpp @@ -1,7 +1,7 @@ -// RUN: clang-tidy %s -checks="-*,cert-err58-cpp" -- -std=c++11 -target x86_64-pc-linux-gnu \ +// RUN: clang-tidy %s -checks="-*,cert-err58-cpp" -- -std=c++17 -target x86_64-pc-linux-gnu \ // RUN: | FileCheck %s -check-prefix=CHECK-EXCEPTIONS \ // RUN: -implicit-check-not="{{warning|error}}:" -// RUN: clang-tidy %s -checks="-*,cert-err58-cpp" -- -DNONEXCEPTIONS -fno-exceptions -std=c++11 -target x86_64-pc-linux-gnu \ +// RUN: clang-tidy %s -checks="-*,cert-err58-cpp" -- -DNONEXCEPTIONS -fno-exceptions -std=c++17 -target x86_64-pc-linux-gnu \ // RUN: | FileCheck %s -allow-empty -check-prefix=CHECK-NONEXCEPTIONS \ // RUN: -implicit-check-not="{{warning|error}}:" @@ -236,3 +236,28 @@ int baz = foo(0); // Not OK; throws at runtime when exceptions are enabled. } // namespace pr35457 #endif // NONEXCEPTIONS +namespace pr39777 { +struct S { S(); }; +struct T { T() noexcept; }; + +auto Okay1 = []{ S s; }; +auto Okay2 = []{ (void)new int; }; +auto NotOkay1 = []{ S s; return 12; }(); // Because the lambda call is not noexcept +// CHECK-EXCEPTIONS: :[[@LINE-1]]:6: warning: initialization of 'NotOkay1' with static storage duration may throw an exception that cannot be caught [cert-err58-cpp] +// CHECK-EXCEPTIONS: :[[@LINE-7]]:12: note: possibly throwing constructor declared here +auto NotOkay2 = []() noexcept { S s; return 12; }(); // Because S::S() is not noexcept +// CHECK-EXCEPTIONS: :[[@LINE-1]]:6: warning: initialization of 'NotOkay2' with static storage duration may throw an exception that cannot be caught [cert-err58-cpp] +// CHECK-EXCEPTIONS: :[[@LINE-10]]:12: note: possibly throwing constructor declared here +auto Okay3 = []() noexcept { T t; return t; }(); + +struct U { + U() noexcept; + auto getBadLambda() const noexcept { + return []{ S s; return s; }; + } +}; +auto Okay4 = []{ U u; return u.getBadLambda(); }(); +auto NotOkay3 = []() noexcept { U u; return u.getBadLambda(); }()(); // Because the lambda returned and called is not noexcept +// CHECK-EXCEPTIONS: :[[@LINE-1]]:6: warning: initialization of 'NotOkay3' with static storage duration may throw an exception that cannot be caught [cert-err58-cpp] +// CHECK-EXCEPTIONS: :[[@LINE-6]]:12: note: possibly throwing function declared here +} |