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/5] [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/5] 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/5] 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/5] 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/5] 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(); } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
