[clang-tools-extra] [clang-tidy] Fix some false negatives in `bugprone-throw-keyword-missing` (PR #173748)
Victor Chernyakin via cfe-commits
cfe-commits at lists.llvm.org
Sat Dec 27 18:29:42 PST 2025
https://github.com/localspook updated https://github.com/llvm/llvm-project/pull/173748
>From c8e7619aff7417d43c053ec8d9daef47ba636632 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Sat, 27 Dec 2025 17:07:06 -0700
Subject: [PATCH] TMP
---
.../bugprone/ThrowKeywordMissingCheck.cpp | 25 +++++++++++--------
.../bugprone/throw-keyword-missing.cpp | 16 +++++++++++-
2 files changed, 30 insertions(+), 11 deletions(-)
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};
+ }
+};
More information about the cfe-commits
mailing list