[clang-tools-extra] Fix #75686: add iter_swap and iter_move to the matched name (PR #76117)

via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 27 17:31:08 PST 2023


https://github.com/Da-Viper updated https://github.com/llvm/llvm-project/pull/76117

>From 97eeda4684804229d9faad19ea7b7888dcd91786 Mon Sep 17 00:00:00 2001
From: Ezike Ebuka <yerimyah1 at gmail.com>
Date: Thu, 21 Dec 2023 02:30:34 +0000
Subject: [PATCH 1/2] Fix #75686: add iter_swap and iter_move to the matched
 name

---
 .../bugprone/ExceptionEscapeCheck.cpp           | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/ExceptionEscapeCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ExceptionEscapeCheck.cpp
index 90bf523ffb00b6..18cd7150185a20 100644
--- a/clang-tools-extra/clang-tidy/bugprone/ExceptionEscapeCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/ExceptionEscapeCheck.cpp
@@ -58,14 +58,15 @@ void ExceptionEscapeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
 
 void ExceptionEscapeCheck::registerMatchers(MatchFinder *Finder) {
   Finder->addMatcher(
-      functionDecl(isDefinition(),
-                   anyOf(isNoThrow(),
-                         allOf(anyOf(cxxDestructorDecl(),
-                                     cxxConstructorDecl(isMoveConstructor()),
-                                     cxxMethodDecl(isMoveAssignmentOperator()),
-                                     isMain(), hasName("swap")),
-                               unless(isExplicitThrow())),
-                         isEnabled(FunctionsThatShouldNotThrow)))
+      functionDecl(
+          isDefinition(),
+          anyOf(isNoThrow(),
+                allOf(anyOf(cxxDestructorDecl(),
+                            cxxConstructorDecl(isMoveConstructor()),
+                            cxxMethodDecl(isMoveAssignmentOperator()), isMain(),
+                            hasAnyName("swap", "iter_swap", "iter_move")),
+                      unless(isExplicitThrow())),
+                isEnabled(FunctionsThatShouldNotThrow)))
           .bind("thrower"),
       this);
 }

>From c7cbf4c9bebbf450410c2679af188672257895aa Mon Sep 17 00:00:00 2001
From: Ezike Ebuka <yerimyah1 at gmail.com>
Date: Thu, 28 Dec 2023 01:30:35 +0000
Subject: [PATCH 2/2] [clang-tidy] Update documentation, release notes and
 tests for bugprone-exception-escape

---
 clang-tools-extra/docs/ReleaseNotes.rst                |  4 ++++
 .../clang-tidy/checks/bugprone/exception-escape.rst    |  2 ++
 .../clang-tidy/checkers/bugprone/exception-escape.cpp  | 10 ++++++++++
 3 files changed, 16 insertions(+)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 6d91748e4cef18..967597cbba11b1 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -242,6 +242,10 @@ Changes in existing checks
   casting during type conversions at variable initialization, now with improved
   compatibility for C++17 and later versions.
 
+- Improved :doc:`bugprone-exception-escape
+  <clang-tidy/checks/bugprone/exception-escape>` check by extending the default
+  check function names to include ``iter_swap`` and ``iter_move``.
+
 - Improved :doc:`bugprone-lambda-function-name
   <clang-tidy/checks/bugprone/lambda-function-name>` check by adding option
   `IgnoreMacros` to ignore warnings in macros.
diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-escape.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-escape.rst
index e6aa8e001492a6..182fade7f47a03 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-escape.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/exception-escape.rst
@@ -11,6 +11,8 @@ should not. The functions which should not throw exceptions are the following:
 * Move assignment operators
 * The ``main()`` functions
 * ``swap()`` functions
+* ``iter_swap()`` functions
+* ``iter_move()`` functions
 * Functions marked with ``throw()`` or ``noexcept``
 * Other functions given as option
 
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape.cpp
index 4a7149e81ce7e5..e20aa267392f55 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape.cpp
@@ -586,6 +586,16 @@ void swap(int&, int&) {
   throw 1;
 }
 
+void iter_swap(int&, int&) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: an exception may be thrown in function 'iter_swap' which should not throw exceptions
+  throw 1;
+}
+
+void iter_move(int&, int&) {
+  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: an exception may be thrown in function 'iter_move' which should not throw exceptions
+  throw 1;
+}
+
 namespace std {
 class bad_alloc {};
 }



More information about the cfe-commits mailing list