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

This patch fixes a false negative in "readability-simplify-boolean-expr".

Previously, the check explicitly ignored 'if` statements that contained an 
initialization statement (example:::, `if (bool x= foo(); x==true)'). this 
limitation caused the check to miss redundant boolean literals in the condition.

CHANGES MADE ! :
-Removed the check for "hasInitStorage()" in the visitor.
-Added a test case to "simplify-boolean-expr.cpp" to verify that cases with 
init-statements are now correctly flaggd and fixed..

this enables clean up of code pattern like:
"if (bool x = func(); x == true)" ----> "if (bool x = func(); x)"

>From 02debe9edb0d2cf6945492b22d748143a1508269 Mon Sep 17 00:00:00 2001
From: Anshul200677 <[email protected]>
Date: Mon, 15 Dec 2025 00:17:32 +0530
Subject: [PATCH] [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) {
+  }
+}

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

Reply via email to