https://github.com/Anshul200677 updated https://github.com/llvm/llvm-project/pull/172220
>From 867eaf36cddcfcf4f04c31cc3e887ee178833c5d Mon Sep 17 00:00:00 2001 From: Anshul200677 <[email protected]> Date: Mon, 15 Dec 2025 00:17:32 +0530 Subject: [PATCH 01/10] [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 f7a2a10a6af692142d413aab3bf48157f28bcd3d Mon Sep 17 00:00:00 2001 From: Anshul200677 <[email protected]> Date: Mon, 15 Dec 2025 13:11:48 +0530 Subject: [PATCH 02/10] 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 c13f7b0b6cf9cabe394aff9686dd218367867b30 Mon Sep 17 00:00:00 2001 From: Anshul200677 <[email protected]> Date: Mon, 15 Dec 2025 13:33:48 +0530 Subject: [PATCH 03/10] 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 41b87ac69beee2298c953ee68bfd990abcb40d58 Mon Sep 17 00:00:00 2001 From: Anshul200677 <[email protected]> Date: Mon, 15 Dec 2025 14:37:59 +0530 Subject: [PATCH 04/10] 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 58010d43e8f14fc13cb3810ffcc12621c4de567a Mon Sep 17 00:00:00 2001 From: Anshul200677 <[email protected]> Date: Mon, 15 Dec 2025 16:13:30 +0530 Subject: [PATCH 05/10] 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 99281e68c3d4791ef0465c9e92d574120772c3dc Mon Sep 17 00:00:00 2001 From: Anshul200677 <[email protected]> Date: Mon, 15 Dec 2025 16:42:26 +0530 Subject: [PATCH 06/10] 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 349462d8674f135453ad3766320507a433bda0ca Mon Sep 17 00:00:00 2001 From: Anshul200677 <[email protected]> Date: Mon, 15 Dec 2025 16:53:26 +0530 Subject: [PATCH 07/10] 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 71dc2be67f20b7666cec630e9c7fc4e8e72b4a86 Mon Sep 17 00:00:00 2001 From: Anshul200677 <[email protected]> Date: Mon, 15 Dec 2025 20:03:39 +0530 Subject: [PATCH 08/10] 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 d2b7642bdf01e..f935875198b5f 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -616,6 +616,11 @@ Changes in existing checks where the check would sometimes suggest deleting not only a redundant ``return`` or ``continue``, but also unrelated lines preceding it. +- 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 ae4bc0e3c1a09a077459ca249623911bc8d5ea05 Mon Sep 17 00:00:00 2001 From: Anshul200677 <[email protected]> Date: Mon, 15 Dec 2025 21:00:06 +0530 Subject: [PATCH 09/10] 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) }; +} >From 9377e92c4916c52a21aa8aa1618e548e114b0821 Mon Sep 17 00:00:00 2001 From: Anshul200677 <[email protected]> Date: Tue, 16 Dec 2025 11:23:36 +0530 Subject: [PATCH 10/10] Update docs and restore C++17 RAII/complex test cases --- .../readability/simplify-boolean-expr.rst | 6 ++++ .../readability/simplify-boolean-expr.cpp | 29 +++++++++++++++---- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability/simplify-boolean-expr.rst b/clang-tools-extra/docs/clang-tidy/checks/readability/simplify-boolean-expr.rst index 5b733e5eaf4d7..eaa56609d3f6f 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/readability/simplify-boolean-expr.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/readability/simplify-boolean-expr.rst @@ -80,6 +80,12 @@ Examples: ``if (x) return true; return false;`` becomes ``return static_cast<bool>(x);`` +.. note:: + + This check properly handles C++17 ``if`` statements with initialization + statements (e.g. ``if (int i = 0; i) ...``). It will not report false positives + or suggest invalid fixes for the condition variable in these cases. + Options ------- 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 d48d16df4dab9..00188d78461a4 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 @@ -1036,6 +1036,14 @@ void instantiate() { ignoreInstantiations<false>(); } +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) }; +} + void if_with_init_statement() { bool x = true; if (bool y = x; y == true) { @@ -1044,10 +1052,19 @@ void if_with_init_statement() { } } -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) }; +// This matches the "RAII" and "Cond" logic from the deleted C++17 file +// to ensure we don't regress or crash on these complex cases. +void test_cxx17_raii_and_complex() { + struct RAII {}; + bool Cond = true; + // Case 1: Init statement with non-boolean type + if (RAII Object; Cond) { + // No warning expected here for now, just verifying no crash. + } + // Case 2: Init statement declaring the condition variable + if (bool X = Cond; X) { + // No warning expected, verifying no crash. + } } + + _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
