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

via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 16 11:52:52 PST 2026


Author: Berkay Sahin
Date: 2026-02-16T22:52:47+03:00
New Revision: f9bca14a33888e70e2f9f1aa1f4a4edd3d7e5dfd

URL: https://github.com/llvm/llvm-project/commit/f9bca14a33888e70e2f9f1aa1f4a4edd3d7e5dfd
DIFF: https://github.com/llvm/llvm-project/commit/f9bca14a33888e70e2f9f1aa1f4a4edd3d7e5dfd.diff

LOG: [clang-tidy] Handle inherited push_back/emplace_back in inefficient-vector-operation (#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

Added: 
    clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation-vectorlike-classes.cpp

Modified: 
    clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst

Removed: 
    


################################################################################
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/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index ee057d9bf0444..2bb4800df47c9 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -218,6 +218,11 @@ Changes in existing checks
 
   - Improved the ignore list to correctly handle ``typedef`` and  ``enum``.
 
+- Improved :doc:`performance-inefficient-vector-operation
+  <clang-tidy/checks/performance/inefficient-vector-operation>` check by
+  correctly handling vector-like classes when ``push_back``/``emplace_back`` are
+  inherited.
+
 - Improved :doc:`performance-move-const-arg
   <clang-tidy/checks/performance/move-const-arg>` check by avoiding false
   positives on trivially copyable types with a non-public copy constructor.

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