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

via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 16 02:12:37 PST 2025


================
@@ -720,18 +720,44 @@ 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()).str();
+
+  // Fix: Ensure the body statement ends with a semicolon if it's not a block.
+  if (!Replacement.empty() && Replacement.back() != ';' &&
+      Replacement.back() != '}')
+    Replacement += ";";
+
+  if (const Stmt *Init = IfStatement->getInit()) {
+    // Fix: Add a space between the init statement and the body.
+    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).str() : "";
+
+  // Fix: Ensure the else statement ends with a semicolon if it exists and isn't
----------------
zeyi2 wrote:

Please remove this comment, same as the other ones.

```suggestion
```

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


More information about the cfe-commits mailing list