[clang-tools-extra] [clang-tidy] Fix FP in bugprone-exception-escape for bodyless non-throwing functions (PR #192658)

via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 17 06:59:35 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tools-extra

Author: Zeyi Xu (zeyi2)

<details>
<summary>Changes</summary>



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


2 Files Affected:

- (modified) clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp (+5) 
- (modified) clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-treat-functions-without-specification-as-throwing.cpp (+27) 


``````````diff
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, "");
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/192658


More information about the cfe-commits mailing list