[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 19:10:15 PST 2025
https://github.com/localspook updated https://github.com/llvm/llvm-project/pull/173748
>From e8ef6f9249224c7ad1de3671cd67001789f00546 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 | 24 +++++++++++--------
clang-tools-extra/docs/ReleaseNotes.rst | 3 ++-
.../bugprone/throw-keyword-missing.cpp | 16 ++++++++++++-
3 files changed, 31 insertions(+), 12 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp
index 9781f0a5ac9de..d3fbea4520817 100644
--- a/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp
@@ -11,24 +11,28 @@
#include "clang/ASTMatchers/ASTMatchFinder.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/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 7b1640594a3d3..4199a91edd357 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -426,7 +426,8 @@ Changes in existing checks
- Improved :doc:`bugprone-throw-keyword-missing
<clang-tidy/checks/bugprone/throw-keyword-missing>` check by only considering
the canonical types of base classes as written and adding a note on the base
- class that triggered the warning.
+ class that triggered the warning. Also, fixed some false negatives in the
+ check.
- Improved :doc:`bugprone-unchecked-optional-access
<clang-tidy/checks/bugprone/unchecked-optional-access>` check by supporting
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