llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tidy

Author: Victor Chernyakin (localspook)

<details>
<summary>Changes</summary>

Fixes #<!-- -->115540.

---
Full diff: https://github.com/llvm/llvm-project/pull/173748.diff


2 Files Affected:

- (modified) clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp 
(+15-10) 
- (modified) 
clang-tools-extra/test/clang-tidy/checkers/bugprone/throw-keyword-missing.cpp 
(+15-1) 


``````````diff
diff --git a/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp
index 9781f0a5ac9de..ff7f238e98df0 100644
--- a/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp
@@ -9,26 +9,31 @@
 #include "ThrowKeywordMissingCheck.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
 
 using namespace clang::ast_matchers;
+using namespace clang::ast_matchers::internal;
 
 namespace clang::tidy::bugprone {
 
 void ThrowKeywordMissingCheck::registerMatchers(MatchFinder *Finder) {
+  const VariadicDynCastAllOfMatcher<Stmt, AttributedStmt> AttributedStmt;
+  // Matches an 'expression-statement' (not to be confused with a statement
+  // expression, the GNU extension!), as defined in [stmt.expr]/1.
+  const auto ExprStmt = [&](const Matcher<Expr> &InnerMatcher) {
+    return expr(hasParent(stmt(anyOf(doStmt(), whileStmt(), forStmt(),
+                                     compoundStmt(), ifStmt(), switchStmt(),
+                                     labelStmt(), AttributedStmt()))),
+                InnerMatcher);
+  };
+
   Finder->addMatcher(
-      cxxConstructExpr(
-          hasType(cxxRecordDecl(anyOf(
+      ExprStmt(
+          cxxConstructExpr(hasType(cxxRecordDecl(anyOf(
               matchesName("[Ee]xception|EXCEPTION"),
               hasAnyBase(hasType(hasCanonicalType(recordType(hasDeclaration(
                   cxxRecordDecl(matchesName("[Ee]xception|EXCEPTION"))
-                      .bind("base"))))))))),
-          unless(anyOf(
-              hasAncestor(
-                  stmt(anyOf(cxxThrowExpr(), callExpr(), returnStmt()))),
-              hasAncestor(decl(anyOf(varDecl(), fieldDecl()))),
-              hasAncestor(expr(cxxNewExpr(hasAnyPlacementArg(anything())))),
-              allOf(hasAncestor(cxxConstructorDecl()),
-                    unless(hasAncestor(cxxCatchStmt()))))))
+                      .bind("base")))))))))))
           .bind("temporary-exception-not-thrown"),
       this);
 }
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/throw-keyword-missing.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/throw-keyword-missing.cpp
index 0ae51780ccc00..52117bceb880e 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/throw-keyword-missing.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/throw-keyword-missing.cpp
@@ -49,7 +49,7 @@ struct RegularException {
 
 // --------------
 
-void stdExceptionNotTrownTest(int i) {
+void stdExceptionNotThrownTest(int i) {
   if (i < 0)
     // CHECK-MESSAGES-DAG: :[[@LINE+1]]:5: warning: suspicious exception 
object created but not thrown; did you mean 'throw {{.*}}'? 
[bugprone-throw-keyword-missing]
     std::exception();
@@ -205,3 +205,17 @@ void placeMentNewTest() {
   alignas(RegularException) unsigned char expr[sizeof(RegularException)];
   new (expr) RegularException{};
 }
+
+void foo() {
+  const auto var = [] {
+    // CHECK-MESSAGES: :[[@LINE+1]]:5: warning: suspicious exception
+    RegularException{0};
+  };
+}
+
+struct Bar {
+  Bar() {
+    // CHECK-MESSAGES: :[[@LINE+1]]:5: warning: suspicious exception
+    RegularException{0};
+  }
+};

``````````

</details>


https://github.com/llvm/llvm-project/pull/173748
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to