[clang-tools-extra] [clang-tidy] Handle inherited push_back/emplace_back in inefficient-vector-operation (PR #181476)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Feb 14 04:58:22 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tidy
@llvm/pr-subscribers-clang-tools-extra
Author: Berkay Sahin (berkaysahiin)
<details>
<summary>Changes</summary>
- Fix to match calls where `push_back`/`emplace_back` are inherited and the implicit object argument is wrapped in implicit casts.
- Add a dedicated regression test for configured vector-like classes.
Fixes #<!-- -->181427
---
Full diff: https://github.com/llvm/llvm-project/pull/181476.diff
2 Files Affected:
- (modified) clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp (+3-3)
- (added) clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation-vectorlike-classes.cpp (+59)
``````````diff
diff --git a/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp b/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp
index 814a4f854319c..8029349891327 100644
--- a/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp
@@ -100,9 +100,9 @@ void InefficientVectorOperationCheck::addMatcher(
.bind(VarDeclStmtName);
const auto AppendCallExpr =
- cxxMemberCallExpr(
- callee(AppendMethodDecl), on(hasType(TargetRecordDecl)),
- onImplicitObjectArgument(declRefExpr(to(TargetVarDecl))))
+ cxxMemberCallExpr(callee(AppendMethodDecl), on(hasType(TargetRecordDecl)),
+ onImplicitObjectArgument(ignoringParenImpCasts(
+ declRefExpr(to(TargetVarDecl)))))
.bind(AppendCallName);
const auto AppendCall = expr(ignoringImplicit(AppendCallExpr));
const auto LoopVarInit = declStmt(hasSingleDecl(
diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation-vectorlike-classes.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation-vectorlike-classes.cpp
new file mode 100644
index 0000000000000..41d2d3733e07a
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation-vectorlike-classes.cpp
@@ -0,0 +1,59 @@
+// RUN: %check_clang_tidy %s performance-inefficient-vector-operation %t -- \
+// RUN: -config='{CheckOptions: \
+// RUN: {performance-inefficient-vector-operation.VectorLikeClasses: \
+// RUN: "VectorLikeInheritedPushBack;VectorLikeDirectPushBack;VectorLikeInheritedEmplaceBack"}}'
+
+class VectorLikePushBackBase {
+public:
+ void push_back(int) {}
+};
+
+class VectorLikeInheritedPushBack : public VectorLikePushBackBase {
+public:
+ void reserve(int);
+};
+
+class VectorLikeDirectPushBack {
+public:
+ void push_back(int) {}
+ void reserve(int) {}
+};
+
+class VectorLikeEmplaceBackBase {
+public:
+ void emplace_back(int) {}
+};
+
+class VectorLikeInheritedEmplaceBack : public VectorLikeEmplaceBackBase {
+public:
+ void reserve(int);
+};
+
+void testVectorLikeClasses() {
+ {
+ VectorLikeInheritedPushBack inheritedPushBackVector;
+ // CHECK-FIXES: inheritedPushBackVector.reserve(100);
+ for (int I = 0; I < 100; ++I) {
+ inheritedPushBackVector.push_back(I);
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside a loop; consider pre-allocating the container capacity before the loop
+ }
+ }
+
+ {
+ VectorLikeDirectPushBack directPushBackVector;
+ // CHECK-FIXES: directPushBackVector.reserve(100);
+ for (int I = 0; I < 100; ++I) {
+ directPushBackVector.push_back(I);
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside a loop; consider pre-allocating the container capacity before the loop
+ }
+ }
+
+ {
+ VectorLikeInheritedEmplaceBack inheritedEmplaceBackVector;
+ // CHECK-FIXES: inheritedEmplaceBackVector.reserve(100);
+ for (int I = 0; I < 100; ++I) {
+ inheritedEmplaceBackVector.emplace_back(I);
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'emplace_back' is called inside a loop; consider pre-allocating the container capacity before the loop
+ }
+ }
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/181476
More information about the cfe-commits
mailing list