https://github.com/Anshul200677 updated 
https://github.com/llvm/llvm-project/pull/172220

>From 02debe9edb0d2cf6945492b22d748143a1508269 Mon Sep 17 00:00:00 2001
From: Anshul200677 <[email protected]>
Date: Mon, 15 Dec 2025 00:17:32 +0530
Subject: [PATCH 1/9] [clang-tidy] Fix readability-simplify-boolean-expr for
 init statements

---
 .../clang-tidy/readability/SimplifyBooleanExprCheck.cpp    | 4 ++--
 .../checkers/readability/simplify-boolean-expr.cpp         | 7 +++++++
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git 
a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
index 1a9c161068030..e1cc21a46d779 100644
--- a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
@@ -357,9 +357,9 @@ class SimplifyBooleanExprCheck::Visitor : public 
RecursiveASTVisitor<Visitor> {
   }
 
   bool VisitIfStmt(IfStmt *If) {
-    // Skip any if's that have a condition var or an init statement, or are
+    // Skiany if's that have a condition var or an init statement, or are
     // "if consteval" statements.
-    if (If->hasInitStorage() || If->hasVarStorage() || If->isConsteval())
+    if ( If->hasVarStorage() || If->isConsteval())
       return true;
     /*
      * if (true) ThenStmt(); -> ThenStmt();
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr.cpp
index 0b99cb89262cd..076847cbf5855 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr.cpp
@@ -1035,3 +1035,10 @@ void instantiate() {
   ignoreInstantiations<true>();
   ignoreInstantiations<false>();
 }
+void if_with_init_statement() {
+  bool x = true;
+  if (bool y = x; y == true) {
+    // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: redundant boolean 
literal supplied to boolean operator [readability-simplify-boolean-expr]
+    // CHECK-FIXES: if (bool y = x; y) {
+  }
+}

>From 1b106762a14d9af9fe09db05f9fa711233a17eaf Mon Sep 17 00:00:00 2001
From: Anshul200677 <[email protected]>
Date: Mon, 15 Dec 2025 13:11:48 +0530
Subject: [PATCH 2/9] Remove redundant test file
 simplify-boolean-expr-cxx17.cpp

---
 .../simplify-boolean-expr-cxx17.cpp           | 19 -------------------
 1 file changed, 19 deletions(-)
 delete mode 100644 
clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr-cxx17.cpp

diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr-cxx17.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr-cxx17.cpp
deleted file mode 100644
index 310afe04672ae..0000000000000
--- 
a/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr-cxx17.cpp
+++ /dev/null
@@ -1,19 +0,0 @@
-// RUN: clang-tidy %s -checks='-*,readability-simplify-boolean-expr' -- 
-std=c++17 | count 0
-struct RAII {};
-bool foo(bool Cond) {
-  bool Result;
-
-  if (RAII Object; Cond)
-    Result = true;
-  else
-    Result = false;
-
-  if (bool X = Cond; X)
-    Result = true;
-  else
-    Result = false;
-
-  if (bool X = Cond; X)
-    return true;
-  return false;
-}

>From cb3ea8690fb26738b6c2a79246ede8839447fa8e Mon Sep 17 00:00:00 2001
From: Anshul200677 <[email protected]>
Date: Mon, 15 Dec 2025 13:33:48 +0530
Subject: [PATCH 3/9] Fix typo in comment

---
 .../clang-tidy/readability/SimplifyBooleanExprCheck.cpp       | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git 
a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
index e1cc21a46d779..f9739c2221a90 100644
--- a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
@@ -357,9 +357,9 @@ class SimplifyBooleanExprCheck::Visitor : public 
RecursiveASTVisitor<Visitor> {
   }
 
   bool VisitIfStmt(IfStmt *If) {
-    // Skiany if's that have a condition var or an init statement, or are
+    // Skip any if's that have a condition var or an init statement, or are
     // "if consteval" statements.
-    if ( If->hasVarStorage() || If->isConsteval())
+    if (If->hasVarStorage() || If->isConsteval())
       return true;
     /*
      * if (true) ThenStmt(); -> ThenStmt();

>From 9906f9c7003db6c37113544a5a294dd16b8758be Mon Sep 17 00:00:00 2001
From: Anshul200677 <[email protected]>
Date: Mon, 15 Dec 2025 14:37:59 +0530
Subject: [PATCH 4/9] Preserve init statement in replacement

---
 .../readability/SimplifyBooleanExprCheck.cpp       | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git 
a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
index f9739c2221a90..4c4b35ef353f2 100644
--- a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
@@ -720,18 +720,24 @@ bool SimplifyBooleanExprCheck::issueDiag(const ASTContext 
&Context,
 void SimplifyBooleanExprCheck::replaceWithThenStatement(
     const ASTContext &Context, const IfStmt *IfStatement,
     const Expr *BoolLiteral) {
+  std::string Replacement = getText(Context, *IfStatement->getThen());
+  if (const Stmt *Init = IfStatement->getInit()) {
+    Replacement = (Twine("{ ") + getText(Context, *Init) + Replacement + " 
}").str();
+  }
   issueDiag(Context, BoolLiteral->getBeginLoc(), SimplifyConditionDiagnostic,
-            IfStatement->getSourceRange(),
-            getText(Context, *IfStatement->getThen()));
+            IfStatement->getSourceRange(), Replacement);
 }
 
 void SimplifyBooleanExprCheck::replaceWithElseStatement(
     const ASTContext &Context, const IfStmt *IfStatement,
     const Expr *BoolLiteral) {
   const Stmt *ElseStatement = IfStatement->getElse();
+  std::string Replacement = ElseStatement ? getText(Context, *ElseStatement) : 
"";
+  if (const Stmt *Init = IfStatement->getInit()) {
+    Replacement = (Twine("{ ") + getText(Context, *Init) + Replacement + " 
}").str();
+  }
   issueDiag(Context, BoolLiteral->getBeginLoc(), SimplifyConditionDiagnostic,
-            IfStatement->getSourceRange(),
-            ElseStatement ? getText(Context, *ElseStatement) : "");
+            IfStatement->getSourceRange(), Replacement);
 }
 
 void SimplifyBooleanExprCheck::replaceWithCondition(

>From 9eacd036861eaac46171ccaee17461218b4ddfef Mon Sep 17 00:00:00 2001
From: Anshul200677 <[email protected]>
Date: Mon, 15 Dec 2025 16:13:30 +0530
Subject: [PATCH 5/9] Fix StringRef to std::string conversion error

---
 .../clang-tidy/readability/SimplifyBooleanExprCheck.cpp     | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git 
a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
index 4c4b35ef353f2..021b35fb20407 100644
--- a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
@@ -720,7 +720,8 @@ bool SimplifyBooleanExprCheck::issueDiag(const ASTContext 
&Context,
 void SimplifyBooleanExprCheck::replaceWithThenStatement(
     const ASTContext &Context, const IfStmt *IfStatement,
     const Expr *BoolLiteral) {
-  std::string Replacement = getText(Context, *IfStatement->getThen());
+  // Added .str() below
+  std::string Replacement = getText(Context, *IfStatement->getThen()).str(); 
   if (const Stmt *Init = IfStatement->getInit()) {
     Replacement = (Twine("{ ") + getText(Context, *Init) + Replacement + " 
}").str();
   }
@@ -732,7 +733,8 @@ void SimplifyBooleanExprCheck::replaceWithElseStatement(
     const ASTContext &Context, const IfStmt *IfStatement,
     const Expr *BoolLiteral) {
   const Stmt *ElseStatement = IfStatement->getElse();
-  std::string Replacement = ElseStatement ? getText(Context, *ElseStatement) : 
"";
+  // Added .str() below
+  std::string Replacement = ElseStatement ? getText(Context, 
*ElseStatement).str() : "";
   if (const Stmt *Init = IfStatement->getInit()) {
     Replacement = (Twine("{ ") + getText(Context, *Init) + Replacement + " 
}").str();
   }

>From b20e73af7b4ae3843776786f1aa70556d66801a8 Mon Sep 17 00:00:00 2001
From: Anshul200677 <[email protected]>
Date: Mon, 15 Dec 2025 16:42:26 +0530
Subject: [PATCH 6/9] Fix code formatting

---
 .../readability/SimplifyBooleanExprCheck.cpp        | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git 
a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
index 021b35fb20407..c36dbe33a0a76 100644
--- a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
@@ -721,9 +721,10 @@ void SimplifyBooleanExprCheck::replaceWithThenStatement(
     const ASTContext &Context, const IfStmt *IfStatement,
     const Expr *BoolLiteral) {
   // Added .str() below
-  std::string Replacement = getText(Context, *IfStatement->getThen()).str(); 
+  std::string Replacement = getText(Context, *IfStatement->getThen()).str();
   if (const Stmt *Init = IfStatement->getInit()) {
-    Replacement = (Twine("{ ") + getText(Context, *Init) + Replacement + " 
}").str();
+    Replacement =
+        (Twine("{ ") + getText(Context, *Init) + Replacement + " }").str();
   }
   issueDiag(Context, BoolLiteral->getBeginLoc(), SimplifyConditionDiagnostic,
             IfStatement->getSourceRange(), Replacement);
@@ -734,14 +735,18 @@ void SimplifyBooleanExprCheck::replaceWithElseStatement(
     const Expr *BoolLiteral) {
   const Stmt *ElseStatement = IfStatement->getElse();
   // Added .str() below
-  std::string Replacement = ElseStatement ? getText(Context, 
*ElseStatement).str() : "";
+  std::string Replacement =
+      ElseStatement ? getText(Context, *ElseStatement).str() : "";
   if (const Stmt *Init = IfStatement->getInit()) {
-    Replacement = (Twine("{ ") + getText(Context, *Init) + Replacement + " 
}").str();
+    Replacement =
+        (Twine("{ ") + getText(Context, *Init) + Replacement + " }").str();
   }
   issueDiag(Context, BoolLiteral->getBeginLoc(), SimplifyConditionDiagnostic,
             IfStatement->getSourceRange(), Replacement);
 }
 
+
+
 void SimplifyBooleanExprCheck::replaceWithCondition(
     const ASTContext &Context, const ConditionalOperator *Ternary,
     bool Negated) {

>From ffb0c3bdd1fbce6175fed0b5c11877edcc78ee11 Mon Sep 17 00:00:00 2001
From: Anshul200677 <[email protected]>
Date: Mon, 15 Dec 2025 16:53:26 +0530
Subject: [PATCH 7/9] Remove extra empty lines

---
 .../clang-tidy/readability/SimplifyBooleanExprCheck.cpp         | 2 --
 1 file changed, 2 deletions(-)

diff --git 
a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
index c36dbe33a0a76..646da8b6328c5 100644
--- a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
@@ -745,8 +745,6 @@ void SimplifyBooleanExprCheck::replaceWithElseStatement(
             IfStatement->getSourceRange(), Replacement);
 }
 
-
-
 void SimplifyBooleanExprCheck::replaceWithCondition(
     const ASTContext &Context, const ConditionalOperator *Ternary,
     bool Negated) {

>From acd0e92c570243c8c4bb01717ed6a381db80faf1 Mon Sep 17 00:00:00 2001
From: Anshul200677 <[email protected]>
Date: Mon, 15 Dec 2025 20:03:39 +0530
Subject: [PATCH 8/9] Remove comments and update ReleaseNotes.rst

---
 .../clang-tidy/readability/SimplifyBooleanExprCheck.cpp      | 2 --
 clang-tools-extra/docs/ReleaseNotes.rst                      | 5 +++++
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git 
a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
index 646da8b6328c5..237abc3edd73d 100644
--- a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
@@ -720,7 +720,6 @@ bool SimplifyBooleanExprCheck::issueDiag(const ASTContext 
&Context,
 void SimplifyBooleanExprCheck::replaceWithThenStatement(
     const ASTContext &Context, const IfStmt *IfStatement,
     const Expr *BoolLiteral) {
-  // Added .str() below
   std::string Replacement = getText(Context, *IfStatement->getThen()).str();
   if (const Stmt *Init = IfStatement->getInit()) {
     Replacement =
@@ -734,7 +733,6 @@ void SimplifyBooleanExprCheck::replaceWithElseStatement(
     const ASTContext &Context, const IfStmt *IfStatement,
     const Expr *BoolLiteral) {
   const Stmt *ElseStatement = IfStatement->getElse();
-  // Added .str() below
   std::string Replacement =
       ElseStatement ? getText(Context, *ElseStatement).str() : "";
   if (const Stmt *Init = IfStatement->getInit()) {
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 6d3da6183c86a..6928fb4a020f1 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -586,6 +586,11 @@ Changes in existing checks
   <clang-tidy/checks/readability/redundant-casting>` check by fixing false
   negatives when explicitly cast from function pointer.
 
+- Improved :doc:`readability-simplify-boolean-expr
+  <clang-tidy/checks/readability/simplify-boolean-expr>` check to avoid
+  invalid code generation when the condition contains an initialization
+  statement.
+
 - Improved :doc:`readability-uppercase-literal-suffix
   <clang-tidy/checks/readability/uppercase-literal-suffix>` check to recognize
   literal suffixes added in C++23 and C23.

>From 573bf1e9d5de91fbbdcfba6516889c1809af1be0 Mon Sep 17 00:00:00 2001
From: Anshul200677 <[email protected]>
Date: Mon, 15 Dec 2025 21:00:06 +0530
Subject: [PATCH 9/9] Add test case for initialization statement and fix
 expected column number

---
 .../checkers/readability/simplify-boolean-expr.cpp    | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr.cpp
index 076847cbf5855..d48d16df4dab9 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr.cpp
@@ -1035,10 +1035,19 @@ void instantiate() {
   ignoreInstantiations<true>();
   ignoreInstantiations<false>();
 }
+
 void if_with_init_statement() {
   bool x = true;
   if (bool y = x; y == true) {
-    // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: redundant boolean 
literal supplied to boolean operator [readability-simplify-boolean-expr]
+    // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: redundant boolean literal 
supplied to boolean operator [readability-simplify-boolean-expr]
     // CHECK-FIXES: if (bool y = x; y) {
   }
 }
+
+void test_init_stmt_true() {
+  void foo(int i);
+  if (int i = 0; true)
+    foo(i);
+  // CHECK-MESSAGES: :[[@LINE-2]]:18: warning: redundant boolean literal in if 
statement condition [readability-simplify-boolean-expr]
+  // CHECK-FIXES:   { int i = 0;foo(i) };
+}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to