[clang-tools-extra] Fix false positive in bugprone-throw-keyword-missing (PR #115302)

Carlos Galvez via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 9 12:46:33 PST 2024


Carlos =?utf-8?q?Gálvez?= <carlos.galvez at zenseact.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/115302 at github.com>


https://github.com/carlosgalvezp updated https://github.com/llvm/llvm-project/pull/115302

>From c6ad9d042612c63cb9862782f17082e07277fc2a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= <carlos.galvez at zenseact.com>
Date: Wed, 6 Nov 2024 08:52:06 +0000
Subject: [PATCH 1/2] Fix false positive in bugprone-throw-keyword-missing

Fixes #115055
---
 .../clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp      | 5 +----
 clang-tools-extra/docs/ReleaseNotes.rst                   | 4 ++++
 .../checkers/bugprone/throw-keyword-missing.cpp           | 8 ++++++++
 3 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp
index 64c155c29cf8b9..17d2e75e4f666f 100644
--- a/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp
@@ -15,9 +15,6 @@ using namespace clang::ast_matchers;
 namespace clang::tidy::bugprone {
 
 void ThrowKeywordMissingCheck::registerMatchers(MatchFinder *Finder) {
-  auto CtorInitializerList =
-      cxxConstructorDecl(hasAnyConstructorInitializer(anything()));
-
   Finder->addMatcher(
       cxxConstructExpr(
           hasType(cxxRecordDecl(
@@ -27,7 +24,7 @@ void ThrowKeywordMissingCheck::registerMatchers(MatchFinder *Finder) {
                   stmt(anyOf(cxxThrowExpr(), callExpr(), returnStmt()))),
               hasAncestor(decl(anyOf(varDecl(), fieldDecl()))),
               hasAncestor(expr(cxxNewExpr(hasAnyPlacementArg(anything())))),
-              allOf(hasAncestor(CtorInitializerList),
+              allOf(hasAncestor(cxxConstructorDecl()),
                     unless(hasAncestor(cxxCatchStmt()))))))
           .bind("temporary-exception-not-thrown"),
       this);
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 51ba157ab05deb..e3294c12896357 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -177,6 +177,10 @@ Changes in existing checks
   usages of ``sizeof()``, ``alignof()``, and ``offsetof()`` when adding or
   subtracting from a pointer directly or when used to scale a numeric value.
 
+- Improved :doc:`bugprone-throw-keyword-missing
+  <clang-tidy/checks/bugprone/throw-keyword-missing>` by fixing a false positive
+  when using non-static member initializers and a constructor.
+
 - Improved :doc:`bugprone-unchecked-optional-access
   <clang-tidy/checks/bugprone/unchecked-optional-access>` to support
   `bsl::optional` and `bdlb::NullableValue` from
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 49233c0deefdf0..aa4bccb109f3f3 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
@@ -139,6 +139,14 @@ CtorInitializerListTest::CtorInitializerListTest(float) try : exc(RegularExcepti
   RegularException();
 }
 
+// https://github.com/llvm/llvm-project/issues/115055
+class CtorInitializerListTest2 {
+ public:
+  CtorInitializerListTest2(){}
+ private:
+  RegularException exc{};
+};
+
 RegularException funcReturningExceptionTest(int i) {
   return RegularException();
 }

>From 9c4caf2ad8b13c721d9b64c96b86da4384edeb35 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= <carlos.galvez at zenseact.com>
Date: Sat, 9 Nov 2024 20:46:23 +0000
Subject: [PATCH 2/2] Fix review comments

---
 .../clang-tidy/checkers/bugprone/throw-keyword-missing.cpp   | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

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 aa4bccb109f3f3..bafd3d19b5a319 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
@@ -139,13 +139,14 @@ CtorInitializerListTest::CtorInitializerListTest(float) try : exc(RegularExcepti
   RegularException();
 }
 
-// https://github.com/llvm/llvm-project/issues/115055
+namespace GH115055 {
 class CtorInitializerListTest2 {
  public:
-  CtorInitializerListTest2(){}
+  CtorInitializerListTest2() {}
  private:
   RegularException exc{};
 };
+}  // namespace GH115055
 
 RegularException funcReturningExceptionTest(int i) {
   return RegularException();



More information about the cfe-commits mailing list