https://github.com/berkaysahiin updated https://github.com/llvm/llvm-project/pull/181476
>From 9085b58541125298be894d6d5b0fe05c8581baff Mon Sep 17 00:00:00 2001 From: Berkay <[email protected]> Date: Sat, 14 Feb 2026 15:39:19 +0300 Subject: [PATCH 1/2] [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 + } + } +} >From 3b7ed0bede9ab130bc4de35aabc0c65cf243dc80 Mon Sep 17 00:00:00 2001 From: Berkay <[email protected]> Date: Sat, 14 Feb 2026 19:53:11 +0300 Subject: [PATCH 2/2] [clang-tidy] Add release note for inherited vector-like handling --- clang-tools-extra/docs/ReleaseNotes.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 5c0060877a67f..d4509086702f2 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -214,6 +214,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. _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
