[clang-tools-extra] [clang-tidy] Handle inherited push_back/emplace_back in inefficient-vector-operation (PR #181476)

Berkay Sahin via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 14 04:57:30 PST 2026


https://github.com/berkaysahiin created https://github.com/llvm/llvm-project/pull/181476

- 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

>From 9085b58541125298be894d6d5b0fe05c8581baff Mon Sep 17 00:00:00 2001
From: Berkay <berkaysahindev at gmail.com>
Date: Sat, 14 Feb 2026 15:39:19 +0300
Subject: [PATCH] [clang-tidy] Handle inherited push_back/emplace_back in
 inefficient-vector-operation

---
 .../InefficientVectorOperationCheck.cpp       |  6 +-
 ...nt-vector-operation-vectorlike-classes.cpp | 59 +++++++++++++++++++
 2 files changed, 62 insertions(+), 3 deletions(-)
 create mode 100644 clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation-vectorlike-classes.cpp

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
+    }
+  }
+}



More information about the cfe-commits mailing list