[clang-tidy][#51939] Exempt placement-new expressions from 'bugprone-throw-keyword...
authorMarkus Böck <markus.boeck02@gmail.com>
Wed, 15 Dec 2021 15:59:04 +0000 (16:59 +0100)
committerMarkus Böck <markus.boeck02@gmail.com>
Wed, 15 Dec 2021 15:59:14 +0000 (16:59 +0100)
The purpose of this checker is to flag a missing throw keyword, and does so by checking for the construction of an exception class that is then unused.
This works great except that placement new expressions are also flagged as those lead to the construction of an object as well, even though they are not temporary (as that is dependent on the storage).
This patch fixes the issue by exempting the match if it is within a placement-new.

Fixes https://github.com/llvm/llvm-project/issues/51939

Differential Revision: https://reviews.llvm.org/D115576

clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/test/clang-tidy/checkers/bugprone-throw-keyword-missing.cpp

index 5327a0c..3f5d875 100644 (file)
@@ -24,11 +24,13 @@ void ThrowKeywordMissingCheck::registerMatchers(MatchFinder *Finder) {
       cxxConstructExpr(
           hasType(cxxRecordDecl(
               isSameOrDerivedFrom(matchesName("[Ee]xception|EXCEPTION")))),
-          unless(anyOf(hasAncestor(stmt(
-                           anyOf(cxxThrowExpr(), callExpr(), returnStmt()))),
-                       hasAncestor(decl(anyOf(varDecl(), fieldDecl()))),
-                       allOf(hasAncestor(CtorInitializerList),
-                             unless(hasAncestor(cxxCatchStmt()))))))
+          unless(anyOf(
+              hasAncestor(
+                  stmt(anyOf(cxxThrowExpr(), callExpr(), returnStmt()))),
+              hasAncestor(decl(anyOf(varDecl(), fieldDecl()))),
+              hasAncestor(expr(cxxNewExpr(hasAnyPlacementArg(anything())))),
+              allOf(hasAncestor(CtorInitializerList),
+                    unless(hasAncestor(cxxCatchStmt()))))))
           .bind("temporary-exception-not-thrown"),
       this);
 }
index 341eb82..bcb9a0f 100644 (file)
@@ -148,6 +148,10 @@ Changes in existing checks
 
 - Fixed a false positive in :doc:`fuchsia-trailing-return
   <clang-tidy/checks/fuchsia-trailing-return>` for C++17 deduction guides.
+  
+- Fixed a false positive in :doc:`bugprone-throw-keyword-missing
+  <clang-tidy/checks/bugprone-throw-keyword-missing>` when creating an exception object
+  using placement new
 
 Removed checks
 ^^^^^^^^^^^^^^
index dff600c..49233c0 100644 (file)
@@ -175,3 +175,14 @@ struct ExceptionRAII {
 void exceptionRAIITest() {
   ExceptionRAII E;
 }
+
+namespace std {
+typedef decltype(sizeof(void*)) size_t;
+}
+
+void* operator new(std::size_t, void*);
+
+void placeMentNewTest() {
+  alignas(RegularException) unsigned char expr[sizeof(RegularException)];
+  new (expr) RegularException{};
+}