Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ 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
// "if consteval" statements.
if (If->hasInitStorage() || If->hasVarStorage() || If->isConsteval())
if (If->hasVarStorage() || If->isConsteval())
return true;
/*
* if (true) ThenStmt(); -> ThenStmt();
Expand Down Expand Up @@ -720,18 +720,40 @@ 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: Check if the statement ends with ; or } (ignoring trailing whitespace)
StringRef Trimmed = StringRef(Replacement).trim();
if (!Trimmed.empty() && Trimmed.back() != ';' && Trimmed.back() != '}')
Replacement += ";";

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).str() : "";

StringRef Trimmed = StringRef(Replacement).trim();
if (!Trimmed.empty() && Trimmed.back() != ';' && Trimmed.back() != '}')
Replacement += ";";

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(
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading