Skip to content

Commit 41b87ac

Browse files
committed
Preserve init statement in replacement
1 parent c13f7b0 commit 41b87ac

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -720,18 +720,24 @@ bool SimplifyBooleanExprCheck::issueDiag(const ASTContext &Context,
720720
void SimplifyBooleanExprCheck::replaceWithThenStatement(
721721
const ASTContext &Context, const IfStmt *IfStatement,
722722
const Expr *BoolLiteral) {
723+
std::string Replacement = getText(Context, *IfStatement->getThen());
724+
if (const Stmt *Init = IfStatement->getInit()) {
725+
Replacement = (Twine("{ ") + getText(Context, *Init) + Replacement + " }").str();
726+
}
723727
issueDiag(Context, BoolLiteral->getBeginLoc(), SimplifyConditionDiagnostic,
724-
IfStatement->getSourceRange(),
725-
getText(Context, *IfStatement->getThen()));
728+
IfStatement->getSourceRange(), Replacement);
726729
}
727730

728731
void SimplifyBooleanExprCheck::replaceWithElseStatement(
729732
const ASTContext &Context, const IfStmt *IfStatement,
730733
const Expr *BoolLiteral) {
731734
const Stmt *ElseStatement = IfStatement->getElse();
735+
std::string Replacement = ElseStatement ? getText(Context, *ElseStatement) : "";
736+
if (const Stmt *Init = IfStatement->getInit()) {
737+
Replacement = (Twine("{ ") + getText(Context, *Init) + Replacement + " }").str();
738+
}
732739
issueDiag(Context, BoolLiteral->getBeginLoc(), SimplifyConditionDiagnostic,
733-
IfStatement->getSourceRange(),
734-
ElseStatement ? getText(Context, *ElseStatement) : "");
740+
IfStatement->getSourceRange(), Replacement);
735741
}
736742

737743
void SimplifyBooleanExprCheck::replaceWithCondition(

0 commit comments

Comments
 (0)