Skip to content

Commit 8103e2a

Browse files
committed
Fix double semicolon generation by ignoring trailing whitespace
1 parent c1c1433 commit 8103e2a

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -722,13 +722,12 @@ void SimplifyBooleanExprCheck::replaceWithThenStatement(
722722
const Expr *BoolLiteral) {
723723
std::string Replacement = getText(Context, *IfStatement->getThen()).str();
724724

725-
// Fix: Ensure the body statement ends with a semicolon if it's not a block.
726-
if (!Replacement.empty() && Replacement.back() != ';' &&
727-
Replacement.back() != '}')
725+
// Fix: Check if the statement ends with ; or } (ignoring trailing whitespace)
726+
StringRef Trimmed = StringRef(Replacement).trim();
727+
if (!Trimmed.empty() && Trimmed.back() != ';' && Trimmed.back() != '}')
728728
Replacement += ";";
729729

730730
if (const Stmt *Init = IfStatement->getInit()) {
731-
// Fix: Add a space between the init statement and the body.
732731
Replacement =
733732
(Twine("{ ") + getText(Context, *Init) + " " + Replacement + " }")
734733
.str();
@@ -744,14 +743,11 @@ void SimplifyBooleanExprCheck::replaceWithElseStatement(
744743
std::string Replacement =
745744
ElseStatement ? getText(Context, *ElseStatement).str() : "";
746745

747-
// Fix: Ensure the else statement ends with a semicolon if it exists and isn't
748-
// a block.
749-
if (!Replacement.empty() && Replacement.back() != ';' &&
750-
Replacement.back() != '}')
746+
StringRef Trimmed = StringRef(Replacement).trim();
747+
if (!Trimmed.empty() && Trimmed.back() != ';' && Trimmed.back() != '}')
751748
Replacement += ";";
752749

753750
if (const Stmt *Init = IfStatement->getInit()) {
754-
// Fix: Add a space between the init statement and the body.
755751
Replacement =
756752
(Twine("{ ") + getText(Context, *Init) + " " + Replacement + " }")
757753
.str();

0 commit comments

Comments
 (0)