[clang-tools-extra] [clang-tidy] Fix readability-simplify-boolean-expr for init statements (PR #172220)

via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 14 11:17:34 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tidy

Author: None (Anshul200677)

<details>
<summary>Changes</summary>

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)"

---
Full diff: https://github.com/llvm/llvm-project/pull/172220.diff


2 Files Affected:

- (modified) clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp (+2-2) 
- (modified) clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr.cpp (+7) 


``````````diff
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) {
+  }
+}

``````````

</details>


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


More information about the cfe-commits mailing list