[clang-tools-extra] [clang-tidy] Fix performance-noexcept-move-constructor for implicit noexcept(false) (PR #226488)

via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 25 07:50:21 PDT 2026


https://github.com/cpp-engine updated https://github.com/llvm/llvm-project/pull/226488

>From b513dc568af3516f0f5eb70245073bfcd055b608 Mon Sep 17 00:00:00 2001
From: Agaev G <cpp.thread at gmail.com>
Date: Fri, 25 Sep 2026 13:04:11 +0000
Subject: [PATCH] [clang-tidy] Fix performance-noexcept-move-constructor for
 implicit noexcept(false)

---
 .../performance/NoexceptFunctionBaseCheck.cpp | 17 ++++++++-------
 .../performance/noexcept-move-constructor.cpp | 21 +++++++++++++++++++
 2 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp
index 33d80615fb19f..32d85e18dff95 100644
--- a/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/NoexceptFunctionBaseCheck.cpp
@@ -26,13 +26,16 @@ void NoexceptFunctionBaseCheck::check(const MatchFinder::MatchResult &Result) {
 
   // Don't complain about nothrow(false), but complain on nothrow(expr)
   // where expr evaluates to false.
-  const auto *ProtoType = FuncDecl->getType()->castAs<FunctionProtoType>();
-  const Expr *NoexceptExpr = ProtoType->getNoexceptExpr();
-  if (NoexceptExpr) {
-    NoexceptExpr = NoexceptExpr->IgnoreImplicit();
-    if (!isa<CXXBoolLiteralExpr>(NoexceptExpr))
-      reportNoexceptEvaluatedToFalse(FuncDecl, NoexceptExpr);
-    return;
+  const auto ExceptionSpecSourceRange = FuncDecl->getExceptionSpecSourceRange();
+  if (ExceptionSpecSourceRange.isValid()) {
+    const auto *ProtoType = FuncDecl->getType()->castAs<FunctionProtoType>();
+    const Expr *NoexceptExpr = ProtoType->getNoexceptExpr();
+    if (NoexceptExpr) {
+      NoexceptExpr = NoexceptExpr->IgnoreImplicit();
+      if (!isa<CXXBoolLiteralExpr>(NoexceptExpr))
+        reportNoexceptEvaluatedToFalse(FuncDecl, NoexceptExpr);
+      return;
+    }
   }
 
   const auto Diag = reportMissingNoexcept(FuncDecl);
diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor.cpp
index 76e1d9e493421..42a1287163d92 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-move-constructor.cpp
@@ -2,6 +2,8 @@
 // RUN: %check_clang_tidy -std=c++17-or-later -check-suffixes=,ERR %s performance-noexcept-move-constructor %t \
 // RUN:                   -- --fix-errors -- -fexceptions -DENABLE_ERROR
 
+#include <utility>
+
 namespace std
 {
   template <typename T>
@@ -181,6 +183,25 @@ struct O : virtual IntWrapper, ThrowOnAnything {
   // CHECK-FIXES: O &operator=(O &&)  noexcept = default;
 };
 
+struct P {
+  P() = default;
+
+  P(P &&) = default;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: move constructors should be marked noexcept [performance-noexcept-move-constructor]
+  // CHECK-FIXES: P(P &&)  noexcept = default;
+  P &operator=(P &&) = default;
+  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: move assignment operators should be marked noexcept [performance-noexcept-move-constructor]
+  // CHECK-FIXES: P &operator=(P &&)  noexcept = default;
+
+  InheritFromThrowOnAnything IFF;
+};
+
+void p() {
+  P P1{};
+  P P2{std::move(P1)};
+  P P3 = std::move(P2);
+}
+
 class OK {};
 
 void f() {



More information about the cfe-commits mailing list