[clang-tools-extra] [clang-tidy] Fix FP in bugprone-exception-escape for bodyless non-throwing functions (PR #192658)
Zeyi Xu via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 17 06:48:32 PDT 2026
https://github.com/zeyi2 created https://github.com/llvm/llvm-project/pull/192658
None
>From 2d644f3c0811e25f20da54ada6fe2f19a5a42f12 Mon Sep 17 00:00:00 2001
From: mtx <mitchell.xu2 at gmail.com>
Date: Fri, 17 Apr 2026 21:48:16 +0800
Subject: [PATCH] [clang-tidy] Fix FP in bugprone-exception-escape for bodyless
non-throwing functions
---
.../clang-tidy/utils/ExceptionAnalyzer.cpp | 5 ++++
...ions-without-specification-as-throwing.cpp | 27 +++++++++++++++++++
2 files changed, 32 insertions(+)
diff --git a/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp b/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
index 8f0d6b1360cd5..af18fda7a3a54 100644
--- a/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
+++ b/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
@@ -502,6 +502,11 @@ ExceptionAnalyzer::ExceptionInfo ExceptionAnalyzer::throwsException(
return Result;
}
+ // Functions without a visible body can still be known non-throwing from their
+ // exception specification.
+ if (!canThrow(Func))
+ return ExceptionInfo::createNonThrowing();
+
auto Result = ExceptionInfo::createUnknown();
if (const auto *FPT = Func->getType()->getAs<FunctionProtoType>()) {
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-treat-functions-without-specification-as-throwing.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-treat-functions-without-specification-as-throwing.cpp
index 9ff54acff6729..ee9f19b5a2896 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-treat-functions-without-specification-as-throwing.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-treat-functions-without-specification-as-throwing.cpp
@@ -11,6 +11,8 @@
// RUN: "bugprone-exception-escape.TreatFunctionsWithoutSpecificationAsThrowing": "None" \
// RUN: }}' -- -fexceptions
+#include <string>
+
void unannotated_no_throw_body() {}
void calls_unannotated() noexcept {
@@ -93,3 +95,28 @@ void calls_explicit_throw() noexcept {
// CHECK-MESSAGES-NONE: :[[@LINE+1]]:3: note: frame #1: function 'calls_explicit_throw' calls function 'explicit_throw' here
explicit_throw();
}
+
+struct ImplicitDtor {
+ ImplicitDtor() = default;
+};
+
+struct DefaultedDtor {
+ DefaultedDtor() = default;
+ ~DefaultedDtor() = default;
+};
+
+struct WithString {
+ WithString(const ImplicitDtor &Implicit, const DefaultedDtor &Defaulted,
+ const std::string &Text)
+ : Implicit(Implicit), Defaulted(Defaulted), Text(Text) {}
+
+ ImplicitDtor Implicit;
+ DefaultedDtor Defaulted;
+ std::string Text;
+};
+
+void constructs_with_string() {
+ ImplicitDtor Implicit;
+ DefaultedDtor Defaulted;
+ WithString Value(Implicit, Defaulted, "");
+}
More information about the cfe-commits
mailing list