Skip to content

Commit df7b90b

Browse files
authored
[clang-tidy][NFC] Refactor bugprone-branch-clone (#171849)
1 parent 04b1975 commit df7b90b

File tree

1 file changed

+14
-42
lines changed

1 file changed

+14
-42
lines changed

clang-tools-extra/clang-tidy/bugprone/BranchCloneCheck.cpp

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,14 @@ using SwitchBranch = llvm::SmallVector<const Stmt *, 2>;
2929
static bool areSwitchBranchesIdentical(const SwitchBranch &LHS,
3030
const SwitchBranch &RHS,
3131
const ASTContext &Context) {
32-
if (LHS.size() != RHS.size())
33-
return false;
34-
35-
for (size_t I = 0, Size = LHS.size(); I < Size; I++) {
32+
return llvm::equal(LHS, RHS, [&](const Stmt *S1, const Stmt *S2) {
3633
// NOTE: We strip goto labels and annotations in addition to stripping
3734
// the `case X:` or `default:` labels, but it is very unlikely that this
3835
// would cause false positives in real-world code.
39-
if (!tidy::utils::areStatementsIdentical(LHS[I]->stripLabelLikeStatements(),
40-
RHS[I]->stripLabelLikeStatements(),
41-
Context)) {
42-
return false;
43-
}
44-
}
45-
46-
return true;
36+
return tidy::utils::areStatementsIdentical(S1->stripLabelLikeStatements(),
37+
S2->stripLabelLikeStatements(),
38+
Context);
39+
});
4740
}
4841

4942
static bool isFallthroughSwitchBranch(const SwitchBranch &Branch) {
@@ -137,19 +130,10 @@ static bool isIdenticalStmt(const ASTContext &Ctx, const Stmt *Stmt1,
137130
return false;
138131

139132
// If all children of two expressions are identical, return true.
140-
Expr::const_child_iterator I1 = Expr1->child_begin();
141-
Expr::const_child_iterator I2 = Expr2->child_begin();
142-
while (I1 != Expr1->child_end() && I2 != Expr2->child_end()) {
143-
if (!isIdenticalStmt(Ctx, *I1, *I2, IgnoreSideEffects))
144-
return false;
145-
++I1;
146-
++I2;
147-
}
148-
// If there are different number of children in the statements, return
149-
// false.
150-
if (I1 != Expr1->child_end())
151-
return false;
152-
if (I2 != Expr2->child_end())
133+
if (!llvm::equal(Expr1->children(), Expr2->children(),
134+
[&](const Stmt *S1, const Stmt *S2) {
135+
return isIdenticalStmt(Ctx, S1, S2, IgnoreSideEffects);
136+
}))
153137
return false;
154138
}
155139

@@ -246,22 +230,10 @@ static bool isIdenticalStmt(const ASTContext &Ctx, const Stmt *Stmt1,
246230
case Stmt::CompoundStmtClass: {
247231
const auto *CompStmt1 = cast<CompoundStmt>(Stmt1);
248232
const auto *CompStmt2 = cast<CompoundStmt>(Stmt2);
249-
250-
if (CompStmt1->size() != CompStmt2->size())
251-
return false;
252-
253-
if (!llvm::all_of(llvm::zip(CompStmt1->body(), CompStmt2->body()),
254-
[&Ctx, IgnoreSideEffects](
255-
std::tuple<const Stmt *, const Stmt *> StmtPair) {
256-
const Stmt *Stmt0 = std::get<0>(StmtPair);
257-
const Stmt *Stmt1 = std::get<1>(StmtPair);
258-
return isIdenticalStmt(Ctx, Stmt0, Stmt1,
259-
IgnoreSideEffects);
260-
})) {
261-
return false;
262-
}
263-
264-
return true;
233+
return llvm::equal(CompStmt1->body(), CompStmt2->body(),
234+
[&](const Stmt *S1, const Stmt *S2) {
235+
return isIdenticalStmt(Ctx, S1, S2, IgnoreSideEffects);
236+
});
265237
}
266238
case Stmt::CompoundAssignOperatorClass:
267239
case Stmt::BinaryOperatorClass: {
@@ -456,7 +428,7 @@ void BranchCloneCheck::check(const MatchFinder::MatchResult &Result) {
456428

457429
diag(BeginCurrent->front()->getBeginLoc(),
458430
"switch has %0 consecutive identical branches")
459-
<< static_cast<int>(std::distance(BeginCurrent, EndCurrent));
431+
<< std::distance(BeginCurrent, EndCurrent);
460432

461433
SourceLocation EndLoc = (EndCurrent - 1)->back()->getEndLoc();
462434
// If the case statement is generated from a macro, it's SourceLocation

0 commit comments

Comments
 (0)