[clang-tools-extra] Added options to readability-implicit-bool-conversion (PR #120087)

via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 18 10:58:40 PST 2024


https://github.com/4m4n-x-B4w4ne updated https://github.com/llvm/llvm-project/pull/120087

>From 03f536888ddc5b7be2514c2d880c6d3119b7f4ee Mon Sep 17 00:00:00 2001
From: 4m4n-x-B4w4ne <125849251+4m4n-x-B4w4ne at users.noreply.github.com>
Date: Mon, 16 Dec 2024 19:43:42 +0530
Subject: [PATCH 01/17] Update ImplicitBoolConversionCheck.cpp

Added new options in ImplicitBoolConversionCheck CheckConversionToBool and CheckConversionFromBool.
---
 .../readability/ImplicitBoolConversionCheck.cpp   | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
index f9fd1d903e231e..517a5d2b982751 100644
--- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
@@ -258,14 +258,17 @@ ImplicitBoolConversionCheck::ImplicitBoolConversionCheck(
     : ClangTidyCheck(Name, Context),
       AllowIntegerConditions(Options.get("AllowIntegerConditions", false)),
       AllowPointerConditions(Options.get("AllowPointerConditions", false)),
-      UseUpperCaseLiteralSuffix(
-          Options.get("UseUpperCaseLiteralSuffix", false)) {}
+      UseUpperCaseLiteralSuffix(Options.get("UseUpperCaseLiteralSuffix", false)),
+      CheckConversionsToBool(Options.get("CheckConversionsToBool",true)),
+      CheckConversionsFromBool(Options.get("CheckConversionsFromBool",true)) {}
 
 void ImplicitBoolConversionCheck::storeOptions(
     ClangTidyOptions::OptionMap &Opts) {
   Options.store(Opts, "AllowIntegerConditions", AllowIntegerConditions);
   Options.store(Opts, "AllowPointerConditions", AllowPointerConditions);
   Options.store(Opts, "UseUpperCaseLiteralSuffix", UseUpperCaseLiteralSuffix);
+  Options.store(Opts,"CheckConversionsToBool",CheckConversionsToBool);
+  Options.store(Opts,"CheckConversionsFromBool",CheckConversionsFromBool);
 }
 
 void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
@@ -358,14 +361,14 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
 void ImplicitBoolConversionCheck::check(
     const MatchFinder::MatchResult &Result) {
 
-  if (const auto *CastToBool =
-          Result.Nodes.getNodeAs<ImplicitCastExpr>("implicitCastToBool")) {
+  if (CheckConversionsToBool && (const auto *CastToBool =
+          Result.Nodes.getNodeAs<ImplicitCastExpr>("implicitCastToBool"))) {
     const auto *Parent = Result.Nodes.getNodeAs<Stmt>("parentStmt");
     return handleCastToBool(CastToBool, Parent, *Result.Context);
   }
 
-  if (const auto *CastFromBool =
-          Result.Nodes.getNodeAs<ImplicitCastExpr>("implicitCastFromBool")) {
+  if (CheckConversionsFromBool && (const auto *CastFromBool =
+          Result.Nodes.getNodeAs<ImplicitCastExpr>("implicitCastFromBool"))) {
     const auto *NextImplicitCast =
         Result.Nodes.getNodeAs<ImplicitCastExpr>("furtherImplicitCast");
     return handleCastFromBool(CastFromBool, NextImplicitCast, *Result.Context);

>From 16c7c95939b4c0c38ebccbbc6cd1da3739244a24 Mon Sep 17 00:00:00 2001
From: 4m4n-x-B4w4ne <125849251+4m4n-x-B4w4ne at users.noreply.github.com>
Date: Mon, 16 Dec 2024 19:45:37 +0530
Subject: [PATCH 02/17] Update ImplicitBoolConversionCheck.h

Added CheckConversionToBool and CheckConversionFromBool Options in the header
---
 .../clang-tidy/readability/ImplicitBoolConversionCheck.h        | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.h b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.h
index 5947f7316e67cc..b0c3c2943e649c 100644
--- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.h
+++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.h
@@ -37,6 +37,8 @@ class ImplicitBoolConversionCheck : public ClangTidyCheck {
   const bool AllowIntegerConditions;
   const bool AllowPointerConditions;
   const bool UseUpperCaseLiteralSuffix;
+  const bool CheckConversionsToBool;
+  const bool CheckConversionsFromBool;
 };
 
 } // namespace clang::tidy::readability

>From 0d6fae8b08a4a365c9295ac8a96de2aba9974c98 Mon Sep 17 00:00:00 2001
From: 4m4n-x-B4w4ne <125849251+4m4n-x-B4w4ne at users.noreply.github.com>
Date: Mon, 16 Dec 2024 19:48:48 +0530
Subject: [PATCH 03/17] Create implicit-bool-conversion-check.cpp

Added new test to check the new options added in the ImplicitBoolConversionCheck.cpp
---
 .../implicit-bool-conversion-check.cpp        | 92 +++++++++++++++++++
 1 file changed, 92 insertions(+)
 create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion-check.cpp

diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion-check.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion-check.cpp
new file mode 100644
index 00000000000000..506769d5a57322
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion-check.cpp
@@ -0,0 +1,92 @@
+// RUN: %check_clang_tidy %s readability-implicit-bool-conversion %t
+
+// RUN: %check_clang_tidy %s readability-implicit-bool-conversion %t \
+// RUN: -- -config='{CheckOptions: [{key: readability-implicit-bool-conversion.CheckConversionsToBool, value: false}, {key: readability-implicit-bool-conversion.CheckConversionsFromBool, value: true}]}'
+
+// RUN: %check_clang_tidy %s readability-implicit-bool-conversion %t \
+// RUN: -- -config='{CheckOptions: [{key: readability-implicit-bool-conversion.CheckConversionsToBool, value: true}, {key: readability-implicit-bool-conversion.CheckConversionsFromBool, value: false}]}'
+
+// RUN: %check_clang_tidy %s readability-implicit-bool-conversion %t \
+// RUN: -- -config='{CheckOptions: [{key: readability-implicit-bool-conversion.CheckConversionsToBool, value: false}, {key: readability-implicit-bool-conversion.CheckConversionsFromBool, value: false}]}'
+
+// ==========================================================
+// Test Case: Conversions to bool (CheckConversionsToBool=true)
+// ==========================================================
+void TestConversionsToBool() {
+  int x = 42;
+  if (x) // CHECK-MESSAGES: :[[@LINE]]:8: warning: implicit conversion 'int' -> 'bool'
+    (void)0;
+
+  float f = 3.14;
+  if (f) // CHECK-MESSAGES: :[[@LINE]]:8: warning: implicit conversion 'float' -> 'bool'
+    (void)0;
+
+  int *p = nullptr;
+  if (p) // CHECK-MESSAGES: :[[@LINE]]:8: warning: implicit conversion 'int *' -> 'bool'
+    (void)0;
+
+  // Pointer-to-member
+  struct S {
+    int member;
+  };
+  int S::*ptr = nullptr;
+  if (ptr) // CHECK-MESSAGES: :[[@LINE]]:8: warning: implicit conversion 'int S::*' -> 'bool'
+    (void)0;
+}
+
+// ==========================================================
+// Test Case: Conversions from bool (CheckConversionsFromBool=true)
+// ==========================================================
+void TestConversionsFromBool() {
+  bool b = true;
+
+  int x = b; // CHECK-MESSAGES: :[[@LINE]]:12: warning: implicit conversion 'bool' -> 'int'
+  float f = b; // CHECK-MESSAGES: :[[@LINE]]:12: warning: implicit conversion 'bool' -> 'float'
+  int *p = b;  // CHECK-MESSAGES: :[[@LINE]]:12: warning: implicit conversion 'bool' -> 'int *'
+
+  struct S {
+    int member;
+  };
+  int S::*ptr = b; // CHECK-MESSAGES: :[[@LINE]]:16: warning: implicit conversion 'bool' -> 'int S::*'
+}
+
+// ==========================================================
+// Test Case: Mixed Configurations (ToBool=false, FromBool=true)
+// ==========================================================
+void TestMixedConfig() {
+  int x = 42;
+  if (x) // No warning: CheckConversionsToBool=false
+    (void)0;
+
+  bool b = true;
+  int y = b; // CHECK-MESSAGES: :[[@LINE]]:12: warning: implicit conversion 'bool' -> 'int'
+}
+
+// ==========================================================
+// Test Case: No Diagnostics (ToBool=false, FromBool=false)
+// ==========================================================
+void TestNoDiagnostics() {
+  int x = 42;
+  if (x) // No warning: CheckConversionsToBool=false
+    (void)0;
+
+  bool b = true;
+  int y = b; // No warning: CheckConversionsFromBool=false
+}
+
+// ==========================================================
+// Test Case: Edge Cases and Complex Expressions
+// ==========================================================
+void TestEdgeCases() {
+  bool b = true;
+
+  // Nested implicit casts
+  int x = (b ? 1 : 0); // CHECK-MESSAGES: :[[@LINE]]:12: warning: implicit conversion 'bool' -> 'int'
+
+  // Function returns implicit bool
+  auto ReturnBool = []() -> bool { return true; };
+  int y = ReturnBool(); // CHECK-MESSAGES: :[[@LINE]]:12: warning: implicit conversion 'bool' -> 'int'
+
+  // Explicit casts (no diagnostics)
+  int z = static_cast<int>(b); // No warning: explicit cast
+}

>From e13bf5ae90fdd52aa0c24a658a107d1c0093c4a0 Mon Sep 17 00:00:00 2001
From: 4m4n-x-B4w4ne <125849251+4m4n-x-B4w4ne at users.noreply.github.com>
Date: Tue, 17 Dec 2024 19:07:54 +0530
Subject: [PATCH 04/17] Update ImplicitBoolConversionCheck.cpp

Fixed Bugs
---
 .../ImplicitBoolConversionCheck.cpp            | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
index 517a5d2b982751..2f79e3e1f9b87f 100644
--- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
@@ -360,20 +360,22 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
 
 void ImplicitBoolConversionCheck::check(
     const MatchFinder::MatchResult &Result) {
-
-  if (CheckConversionsToBool && (const auto *CastToBool =
-          Result.Nodes.getNodeAs<ImplicitCastExpr>("implicitCastToBool"))) {
+  if(CheckConversionsToBool){
+  if (const auto *CastToBool =
+          Result.Nodes.getNodeAs<ImplicitCastExpr>("implicitCastToBool")) {
     const auto *Parent = Result.Nodes.getNodeAs<Stmt>("parentStmt");
     return handleCastToBool(CastToBool, Parent, *Result.Context);
   }
-
-  if (CheckConversionsFromBool && (const auto *CastFromBool =
-          Result.Nodes.getNodeAs<ImplicitCastExpr>("implicitCastFromBool"))) {
-    const auto *NextImplicitCast =
+  }
+  if(CheckConversionsFromBool ){
+    if (const auto *CastFromBool =
+          Result.Nodes.getNodeAs<ImplicitCastExpr>("implicitCastFromBool")) {
+      const auto *NextImplicitCast =
         Result.Nodes.getNodeAs<ImplicitCastExpr>("furtherImplicitCast");
-    return handleCastFromBool(CastFromBool, NextImplicitCast, *Result.Context);
+      return handleCastFromBool(CastFromBool, NextImplicitCast, *Result.Context);
   }
 }
+}
 
 void ImplicitBoolConversionCheck::handleCastToBool(const ImplicitCastExpr *Cast,
                                                    const Stmt *Parent,

>From a480460b576730757c14caf34604b7039b07f711 Mon Sep 17 00:00:00 2001
From: 4m4n-x-B4w4ne <125849251+4m4n-x-B4w4ne at users.noreply.github.com>
Date: Tue, 17 Dec 2024 19:40:11 +0530
Subject: [PATCH 05/17] Update implicit-bool-conversion-check.cpp

Fixed Bugs in test .
---
 .../checkers/readability/implicit-bool-conversion-check.cpp | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion-check.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion-check.cpp
index 506769d5a57322..32ea1b98e74a7e 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion-check.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/implicit-bool-conversion-check.cpp
@@ -42,12 +42,6 @@ void TestConversionsFromBool() {
 
   int x = b; // CHECK-MESSAGES: :[[@LINE]]:12: warning: implicit conversion 'bool' -> 'int'
   float f = b; // CHECK-MESSAGES: :[[@LINE]]:12: warning: implicit conversion 'bool' -> 'float'
-  int *p = b;  // CHECK-MESSAGES: :[[@LINE]]:12: warning: implicit conversion 'bool' -> 'int *'
-
-  struct S {
-    int member;
-  };
-  int S::*ptr = b; // CHECK-MESSAGES: :[[@LINE]]:16: warning: implicit conversion 'bool' -> 'int S::*'
 }
 
 // ==========================================================

>From 5d1e7fc192fae0fffc3ba801f682a7f01a6f4e83 Mon Sep 17 00:00:00 2001
From: 4m4n-x-B4w4ne <125849251+4m4n-x-B4w4ne at users.noreply.github.com>
Date: Wed, 18 Dec 2024 09:10:42 +0530
Subject: [PATCH 06/17] Update ReleaseNotes.rst

Added new Options in ImplicitBoolConversionCheck.
---
 clang-tools-extra/docs/ReleaseNotes.rst | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 6803842106791b..2ddfdd6b7b1a27 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -332,6 +332,11 @@ Changes in existing checks
   <clang-tidy/checks/readability/identifier-naming>` check to
   validate ``namespace`` aliases.
 
+-Improved :doc: `readability-implicit-bool-conversion
+ <clang-tidy/checks/readability/implicit-bool-conversion>`
+  - `CheckConversionsToBool`: Allows enabling or disabling warnings for implicit conversions to `bool` (default) : true .
+  - `CheckConversionsFromBool`: Allows enabling or disabling warnings for implicit conversions from `bool` (default) : true.
+
 Removed checks
 ^^^^^^^^^^^^^^
 

>From 9e83c0dd9f202b6488f30c89e2505d50658de4b5 Mon Sep 17 00:00:00 2001
From: 4m4n-x-B4w4ne <125849251+4m4n-x-B4w4ne at users.noreply.github.com>
Date: Wed, 18 Dec 2024 20:27:23 +0530
Subject: [PATCH 07/17] Update ReleaseNotes.rst

---
 clang-tools-extra/docs/ReleaseNotes.rst | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 2ddfdd6b7b1a27..eea616fcde28ae 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -319,10 +319,10 @@ Changes in existing checks
   diagnostic.
 
 - Improved :doc:`readability-implicit-bool-conversion
-  <clang-tidy/checks/readability/implicit-bool-conversion>` check
-  by adding the option `UseUpperCaseLiteralSuffix` to select the
-  case of the literal suffix in fixes and fixing false positive for implicit
-  conversion of comparison result in C23.
+  <clang-tidy/checks/readability/implicit-bool-conversion>` 
+   - `UseUpperCaseLiteralSuffix` : check by adding the option to select the case of the literal suffix in fixes and fixing false positive for implicit conversion of comparison result in C23.
+   - `CheckConversionsToBool`: Allows enabling or disabling warnings for implicit conversions to `bool` .
+   - `CheckConversionsFromBool`: Allows enabling or disabling warnings for implicit conversions from `bool`.
 
 - Improved :doc:`readability-redundant-smartptr-get
   <clang-tidy/checks/readability/redundant-smartptr-get>` check to
@@ -332,11 +332,6 @@ Changes in existing checks
   <clang-tidy/checks/readability/identifier-naming>` check to
   validate ``namespace`` aliases.
 
--Improved :doc: `readability-implicit-bool-conversion
- <clang-tidy/checks/readability/implicit-bool-conversion>`
-  - `CheckConversionsToBool`: Allows enabling or disabling warnings for implicit conversions to `bool` (default) : true .
-  - `CheckConversionsFromBool`: Allows enabling or disabling warnings for implicit conversions from `bool` (default) : true.
-
 Removed checks
 ^^^^^^^^^^^^^^
 

>From 4b75aff9e7df63368542a800b996a2283e00db50 Mon Sep 17 00:00:00 2001
From: 4m4n-x-B4w4ne <125849251+4m4n-x-B4w4ne at users.noreply.github.com>
Date: Wed, 18 Dec 2024 20:31:57 +0530
Subject: [PATCH 08/17] Update ReleaseNotes.rst

---
 clang-tools-extra/docs/ReleaseNotes.rst | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index eea616fcde28ae..9c72f380b87459 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -321,8 +321,8 @@ Changes in existing checks
 - Improved :doc:`readability-implicit-bool-conversion
   <clang-tidy/checks/readability/implicit-bool-conversion>` 
    - `UseUpperCaseLiteralSuffix` : check by adding the option to select the case of the literal suffix in fixes and fixing false positive for implicit conversion of comparison result in C23.
-   - `CheckConversionsToBool`: Allows enabling or disabling warnings for implicit conversions to `bool` .
-   - `CheckConversionsFromBool`: Allows enabling or disabling warnings for implicit conversions from `bool`.
+   - `CheckConversionsToBool`: check by adding the option to allow enabling or disabling warnings for implicit conversions to `bool` .
+   - `CheckConversionsFromBool`: check by adding the option to allow enabling or disabling warnings for implicit conversions from `bool`.
 
 - Improved :doc:`readability-redundant-smartptr-get
   <clang-tidy/checks/readability/redundant-smartptr-get>` check to

>From dc67c7f4e2a064d77f5f9d1b3b65cd30b2b74ec7 Mon Sep 17 00:00:00 2001
From: 4m4n-x-B4w4ne <125849251+4m4n-x-B4w4ne at users.noreply.github.com>
Date: Wed, 18 Dec 2024 20:37:19 +0530
Subject: [PATCH 09/17] Update ReleaseNotes.rst

---
 clang-tools-extra/docs/ReleaseNotes.rst | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 9c72f380b87459..01925f77f83700 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -320,9 +320,10 @@ Changes in existing checks
 
 - Improved :doc:`readability-implicit-bool-conversion
   <clang-tidy/checks/readability/implicit-bool-conversion>` 
-   - `UseUpperCaseLiteralSuffix` : check by adding the option to select the case of the literal suffix in fixes and fixing false positive for implicit conversion of comparison result in C23.
-   - `CheckConversionsToBool`: check by adding the option to allow enabling or disabling warnings for implicit conversions to `bool` .
-   - `CheckConversionsFromBool`: check by adding the option to allow enabling or disabling warnings for implicit conversions from `bool`.
+   Check by adding the option 
+   - `UseUpperCaseLiteralSuffix` : to select the case of the literal suffix in fixes and fixing false positive for implicit conversion of comparison result in C23.
+   - `CheckConversionsToBool`: to allow enabling or disabling warnings for implicit conversions to `bool` .
+   - `CheckConversionsFromBool`: to allow enabling or disabling warnings for implicit conversions from `bool`.
 
 - Improved :doc:`readability-redundant-smartptr-get
   <clang-tidy/checks/readability/redundant-smartptr-get>` check to

>From c6078dc86d0c044c549efc0c7a30324a4e3293f5 Mon Sep 17 00:00:00 2001
From: 4m4n-x-B4w4ne <125849251+4m4n-x-B4w4ne at users.noreply.github.com>
Date: Wed, 18 Dec 2024 20:47:37 +0530
Subject: [PATCH 10/17] Update ReleaseNotes.rst

---
 clang-tools-extra/docs/ReleaseNotes.rst | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 01925f77f83700..b0b90bd47df4fe 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -321,9 +321,13 @@ Changes in existing checks
 - Improved :doc:`readability-implicit-bool-conversion
   <clang-tidy/checks/readability/implicit-bool-conversion>` 
    Check by adding the option 
-   - `UseUpperCaseLiteralSuffix` : to select the case of the literal suffix in fixes and fixing false positive for implicit conversion of comparison result in C23.
-   - `CheckConversionsToBool`: to allow enabling or disabling warnings for implicit conversions to `bool` .
-   - `CheckConversionsFromBool`: to allow enabling or disabling warnings for implicit conversions from `bool`.
+   - `UseUpperCaseLiteralSuffix` : to select the case of the literal suffix in 
+      fixes and fixing false positive for implicit conversion of comparison 
+      result in C23.
+   - `CheckConversionsToBool`: to allow enabling or disabling warnings for 
+      implicit conversions to `bool` .
+   - `CheckConversionsFromBool`: to allow enabling or disabling warnings for 
+      implicit conversions from `bool`.
 
 - Improved :doc:`readability-redundant-smartptr-get
   <clang-tidy/checks/readability/redundant-smartptr-get>` check to

>From 0882713138cf5139ac82fa64aa9a56865d9716d8 Mon Sep 17 00:00:00 2001
From: 4m4n-x-B4w4ne <125849251+4m4n-x-B4w4ne at users.noreply.github.com>
Date: Wed, 18 Dec 2024 20:51:44 +0530
Subject: [PATCH 11/17] Update ReleaseNotes.rst

---
 clang-tools-extra/docs/ReleaseNotes.rst | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index b0b90bd47df4fe..6322b84aa84713 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -319,8 +319,8 @@ Changes in existing checks
   diagnostic.
 
 - Improved :doc:`readability-implicit-bool-conversion
-  <clang-tidy/checks/readability/implicit-bool-conversion>` 
-   Check by adding the option 
+  <clang-tidy/checks/readability/implicit-bool-conversion>` check by adding 
+   the option 
    - `UseUpperCaseLiteralSuffix` : to select the case of the literal suffix in 
       fixes and fixing false positive for implicit conversion of comparison 
       result in C23.

>From d59f00053c71bdff9e5f49e1cc3d0df139b2be0c Mon Sep 17 00:00:00 2001
From: 4m4n-x-B4w4ne <125849251+4m4n-x-B4w4ne at users.noreply.github.com>
Date: Wed, 18 Dec 2024 21:00:52 +0530
Subject: [PATCH 12/17] Update ReleaseNotes.rst

---
 clang-tools-extra/docs/ReleaseNotes.rst | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 6322b84aa84713..127be656b3477b 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -320,14 +320,14 @@ Changes in existing checks
 
 - Improved :doc:`readability-implicit-bool-conversion
   <clang-tidy/checks/readability/implicit-bool-conversion>` check by adding 
-   the option 
-   - `UseUpperCaseLiteralSuffix` : to select the case of the literal suffix in 
+   the options: 
+   - `UseUpperCaseLiteralSuffix`: to select the case of the literal suffix in 
       fixes and fixing false positive for implicit conversion of comparison 
       result in C23.
    - `CheckConversionsToBool`: to allow enabling or disabling warnings for 
-      implicit conversions to `bool` .
+      implicit conversions to ``bool`` .
    - `CheckConversionsFromBool`: to allow enabling or disabling warnings for 
-      implicit conversions from `bool`.
+      implicit conversions from ``bool``.
 
 - Improved :doc:`readability-redundant-smartptr-get
   <clang-tidy/checks/readability/redundant-smartptr-get>` check to

>From 4520dd67fa2b256f21119313dd027056aa273a06 Mon Sep 17 00:00:00 2001
From: 4m4n-x-B4w4ne <125849251+4m4n-x-B4w4ne at users.noreply.github.com>
Date: Wed, 18 Dec 2024 21:05:11 +0530
Subject: [PATCH 13/17] Update ReleaseNotes.rst

---
 clang-tools-extra/docs/ReleaseNotes.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 127be656b3477b..afa89ce4a807a3 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -325,7 +325,7 @@ Changes in existing checks
       fixes and fixing false positive for implicit conversion of comparison 
       result in C23.
    - `CheckConversionsToBool`: to allow enabling or disabling warnings for 
-      implicit conversions to ``bool`` .
+      implicit conversions to ``bool``.
    - `CheckConversionsFromBool`: to allow enabling or disabling warnings for 
       implicit conversions from ``bool``.
 

>From a9376eae33c7cceafff8588c72996462ae41b28a Mon Sep 17 00:00:00 2001
From: 4m4n-x-B4w4ne <125849251+4m4n-x-B4w4ne at users.noreply.github.com>
Date: Wed, 18 Dec 2024 21:09:33 +0530
Subject: [PATCH 14/17] Update ReleaseNotes.rst

Improved Preview By adding blank line.
---
 clang-tools-extra/docs/ReleaseNotes.rst | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index afa89ce4a807a3..9c53a770a5edef 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -319,8 +319,9 @@ Changes in existing checks
   diagnostic.
 
 - Improved :doc:`readability-implicit-bool-conversion
-  <clang-tidy/checks/readability/implicit-bool-conversion>` check by adding 
-   the options: 
+  <clang-tidy/checks/readability/implicit-bool-conversion>` check by adding the
+   options:
+
    - `UseUpperCaseLiteralSuffix`: to select the case of the literal suffix in 
       fixes and fixing false positive for implicit conversion of comparison 
       result in C23.

>From dc229ca60262bf425fdc6e5dcd0621ab447597a9 Mon Sep 17 00:00:00 2001
From: 4m4n-x-B4w4ne <125849251+4m4n-x-B4w4ne at users.noreply.github.com>
Date: Wed, 18 Dec 2024 23:19:04 +0530
Subject: [PATCH 15/17] Update ImplicitBoolConversionCheck.cpp

Fixed Bugs .
---
 .../readability/ImplicitBoolConversionCheck.cpp  | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
index 2f79e3e1f9b87f..4d6a966f71c745 100644
--- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
@@ -322,7 +322,8 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
                    // additional parens in replacement.
                    optionally(hasParent(stmt().bind("parentStmt"))),
                    unless(isInTemplateInstantiation()),
-                   unless(IsInCompilerGeneratedFunction))
+                   unless(IsInCompilerGeneratedFunction),
+                   unless(!CheckConversionToBool)
                    .bind("implicitCastToBool")),
       this);
 
@@ -348,33 +349,32 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
               unless(hasParent(
                   binaryOperator(anyOf(BoolComparison, BoolXor,
                                        BoolOpAssignment, BitfieldAssignment)))),
-              implicitCastExpr().bind("implicitCastFromBool"),
               unless(hasParent(BitfieldConstruct)),
               // Check also for nested casts, for example: bool -> int -> float.
               anyOf(hasParent(implicitCastExpr().bind("furtherImplicitCast")),
                     anything()),
               unless(isInTemplateInstantiation()),
-              unless(IsInCompilerGeneratedFunction))),
+              unless(IsInCompilerGeneratedFunction),
+              unless(!CheckConversionFromBool))
+              .bind("implicitCastFromBool")),
       this);
 }
 
 void ImplicitBoolConversionCheck::check(
     const MatchFinder::MatchResult &Result) {
-  if(CheckConversionsToBool){
+  
   if (const auto *CastToBool =
           Result.Nodes.getNodeAs<ImplicitCastExpr>("implicitCastToBool")) {
     const auto *Parent = Result.Nodes.getNodeAs<Stmt>("parentStmt");
     return handleCastToBool(CastToBool, Parent, *Result.Context);
   }
-  }
-  if(CheckConversionsFromBool ){
-    if (const auto *CastFromBool =
+  if (const auto *CastFromBool =
           Result.Nodes.getNodeAs<ImplicitCastExpr>("implicitCastFromBool")) {
       const auto *NextImplicitCast =
         Result.Nodes.getNodeAs<ImplicitCastExpr>("furtherImplicitCast");
       return handleCastFromBool(CastFromBool, NextImplicitCast, *Result.Context);
   }
-}
+
 }
 
 void ImplicitBoolConversionCheck::handleCastToBool(const ImplicitCastExpr *Cast,

>From f6efcaa256418db9024119e500c0038060d7afe6 Mon Sep 17 00:00:00 2001
From: 4m4n-x-B4w4ne <125849251+4m4n-x-B4w4ne at users.noreply.github.com>
Date: Thu, 19 Dec 2024 00:17:29 +0530
Subject: [PATCH 16/17] Update ImplicitBoolConversionCheck.cpp

Fixed Bugs .
---
 .../ImplicitBoolConversionCheck.cpp           | 42 ++++++++++++-------
 1 file changed, 26 insertions(+), 16 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
index 4d6a966f71c745..acd1921f4eac2e 100644
--- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
@@ -272,6 +272,7 @@ void ImplicitBoolConversionCheck::storeOptions(
 }
 
 void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
+  if(CheckConversionFromBool || CheckConversionToBool){
   auto ExceptionCases =
       expr(anyOf(allOf(isMacroExpansion(), unless(isNULLMacroExpansion())),
                  has(ignoringImplicit(
@@ -280,23 +281,18 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
                  expr(hasType(qualType().bind("type")),
                       hasParent(initListExpr(hasParent(explicitCastExpr(
                           hasType(qualType(equalsBoundNode("type"))))))))));
-  auto ImplicitCastFromBool = implicitCastExpr(
-      anyOf(hasCastKind(CK_IntegralCast), hasCastKind(CK_IntegralToFloating),
-            // Prior to C++11 cast from bool literal to pointer was allowed.
-            allOf(anyOf(hasCastKind(CK_NullToPointer),
-                        hasCastKind(CK_NullToMemberPointer)),
-                  hasSourceExpression(cxxBoolLiteral()))),
-      hasSourceExpression(expr(hasType(booleanType()))));
+    
   auto BoolXor =
       binaryOperator(hasOperatorName("^"), hasLHS(ImplicitCastFromBool),
                      hasRHS(ImplicitCastFromBool));
+  auto IsInCompilerGeneratedFunction = hasAncestor(namedDecl(anyOf(
+      isImplicit(), functionDecl(isDefaulted()), functionTemplateDecl())));
+    
+  if(CheckConversionToBool){
   auto ComparisonInCall = allOf(
       hasParent(callExpr()),
       hasSourceExpression(binaryOperator(hasAnyOperatorName("==", "!="))));
-
-  auto IsInCompilerGeneratedFunction = hasAncestor(namedDecl(anyOf(
-      isImplicit(), functionDecl(isDefaulted()), functionTemplateDecl())));
-
+    
   Finder->addMatcher(
       traverse(TK_AsIs,
                implicitCastExpr(
@@ -322,21 +318,34 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
                    // additional parens in replacement.
                    optionally(hasParent(stmt().bind("parentStmt"))),
                    unless(isInTemplateInstantiation()),
-                   unless(IsInCompilerGeneratedFunction),
-                   unless(!CheckConversionToBool)
+                   unless(IsInCompilerGeneratedFunction))
                    .bind("implicitCastToBool")),
       this);
+  }
 
+  if(CheckConversionFromBool){
+  auto ImplicitCastFromBool = implicitCastExpr(
+      anyOf(hasCastKind(CK_IntegralCast), hasCastKind(CK_IntegralToFloating),
+            // Prior to C++11 cast from bool literal to pointer was allowed.
+            allOf(anyOf(hasCastKind(CK_NullToPointer),
+                        hasCastKind(CK_NullToMemberPointer)),
+                  hasSourceExpression(cxxBoolLiteral()))),
+      hasSourceExpression(expr(hasType(booleanType()))));
+  
   auto BoolComparison = binaryOperator(hasAnyOperatorName("==", "!="),
                                        hasLHS(ImplicitCastFromBool),
                                        hasRHS(ImplicitCastFromBool));
+  
   auto BoolOpAssignment = binaryOperator(hasAnyOperatorName("|=", "&="),
                                          hasLHS(expr(hasType(booleanType()))));
+  
   auto BitfieldAssignment = binaryOperator(
       hasLHS(memberExpr(hasDeclaration(fieldDecl(hasBitWidth(1))))));
+  
   auto BitfieldConstruct = cxxConstructorDecl(hasDescendant(cxxCtorInitializer(
       withInitializer(equalsBoundNode("implicitCastFromBool")),
       forField(hasBitWidth(1)))));
+  
   Finder->addMatcher(
       traverse(
           TK_AsIs,
@@ -349,15 +358,16 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
               unless(hasParent(
                   binaryOperator(anyOf(BoolComparison, BoolXor,
                                        BoolOpAssignment, BitfieldAssignment)))),
+              implictCastExpr().bind("implicitCastFromBool"),
               unless(hasParent(BitfieldConstruct)),
               // Check also for nested casts, for example: bool -> int -> float.
               anyOf(hasParent(implicitCastExpr().bind("furtherImplicitCast")),
                     anything()),
               unless(isInTemplateInstantiation()),
-              unless(IsInCompilerGeneratedFunction),
-              unless(!CheckConversionFromBool))
-              .bind("implicitCastFromBool")),
+              unless(IsInCompilerGeneratedFunction))),
       this);
+  }
+  }
 }
 
 void ImplicitBoolConversionCheck::check(

>From ac54d56296cd69669943d1c3d947ab743ef7f2ba Mon Sep 17 00:00:00 2001
From: 4m4n-x-B4w4ne <125849251+4m4n-x-B4w4ne at users.noreply.github.com>
Date: Thu, 19 Dec 2024 00:28:22 +0530
Subject: [PATCH 17/17] Update ImplicitBoolConversionCheck.cpp

Fixed Bugs.
---
 .../readability/ImplicitBoolConversionCheck.cpp  | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
index acd1921f4eac2e..383a1307aa8d81 100644
--- a/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ImplicitBoolConversionCheck.cpp
@@ -282,6 +282,13 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
                       hasParent(initListExpr(hasParent(explicitCastExpr(
                           hasType(qualType(equalsBoundNode("type"))))))))));
     
+  auto ImplicitCastFromBool = implicitCastExpr(
+      anyOf(hasCastKind(CK_IntegralCast), hasCastKind(CK_IntegralToFloating),
+            // Prior to C++11 cast from bool literal to pointer was allowed.
+            allOf(anyOf(hasCastKind(CK_NullToPointer),
+                        hasCastKind(CK_NullToMemberPointer)),
+                  hasSourceExpression(cxxBoolLiteral()))),
+      hasSourceExpression(expr(hasType(booleanType()))));
   auto BoolXor =
       binaryOperator(hasOperatorName("^"), hasLHS(ImplicitCastFromBool),
                      hasRHS(ImplicitCastFromBool));
@@ -324,13 +331,6 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
   }
 
   if(CheckConversionFromBool){
-  auto ImplicitCastFromBool = implicitCastExpr(
-      anyOf(hasCastKind(CK_IntegralCast), hasCastKind(CK_IntegralToFloating),
-            // Prior to C++11 cast from bool literal to pointer was allowed.
-            allOf(anyOf(hasCastKind(CK_NullToPointer),
-                        hasCastKind(CK_NullToMemberPointer)),
-                  hasSourceExpression(cxxBoolLiteral()))),
-      hasSourceExpression(expr(hasType(booleanType()))));
   
   auto BoolComparison = binaryOperator(hasAnyOperatorName("==", "!="),
                                        hasLHS(ImplicitCastFromBool),
@@ -358,7 +358,7 @@ void ImplicitBoolConversionCheck::registerMatchers(MatchFinder *Finder) {
               unless(hasParent(
                   binaryOperator(anyOf(BoolComparison, BoolXor,
                                        BoolOpAssignment, BitfieldAssignment)))),
-              implictCastExpr().bind("implicitCastFromBool"),
+              implicitCastExpr().bind("implicitCastFromBool"),
               unless(hasParent(BitfieldConstruct)),
               // Check also for nested casts, for example: bool -> int -> float.
               anyOf(hasParent(implicitCastExpr().bind("furtherImplicitCast")),



More information about the cfe-commits mailing list