https://github.com/llukito created https://github.com/llvm/llvm-project/pull/179484
### Summary This change allows the `performance-inefficient-vector-operation` check to detect `push_back` or `emplace_back` calls even when the loop body contains multiple statements. Previously, the check was limited to loops with a single statement. ### Logic Changes - Removed the `statementCountIs(1)` constraint from the AST matcher. - Updated the matcher to check for `AppendCall` within a `compoundStmt`. ### Testing - Added a regression test in `clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp >From e0d915f68f6994ee4987f230020a290e49833038 Mon Sep 17 00:00:00 2001 From: Luka Aladashvili <[email protected]> Date: Tue, 3 Feb 2026 15:32:33 +0000 Subject: [PATCH] [clang-tidy] Fix false negative in performance-inefficient-vector-operation --- .../performance/InefficientVectorOperationCheck.cpp | 4 ++-- .../performance/inefficient-vector-operation.cpp | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp b/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp index 814a4f854319c..8f8bbbfd10937 100644 --- a/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp @@ -111,10 +111,10 @@ void InefficientVectorOperationCheck::addMatcher( const auto RefersToLoopVar = ignoringParenImpCasts( declRefExpr(to(varDecl(equalsBoundNode(LoopInitVarName))))); - // Matchers for the loop whose body has only 1 push_back/emplace_back calling + // Matchers for the loop whose body contains a push_back/emplace_back calling // statement. const auto HasInterestingLoopBody = hasBody( - anyOf(compoundStmt(statementCountIs(1), has(AppendCall)), AppendCall)); + anyOf(compoundStmt(has(AppendCall)), AppendCall)); const auto InInterestingCompoundStmt = hasParent(compoundStmt(has(TargetVarDefStmt)).bind(LoopParentName)); diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp index e1e25d76d4909..631354605e915 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp @@ -424,3 +424,16 @@ void f(std::vector<int>& t) { } } // namespace gh95596 + +void multiple_statements_in_loop() { + std::vector<int> v1; + std::vector<int> v2; + // CHECK-FIXES: v1.reserve(10); + // CHECK-FIXES: v2.reserve(10); + for (int i = 0; i < 10; ++i) { + v1.push_back(i); + // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'push_back' is called inside a loop + v2.push_back(i); + // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'push_back' is called inside a loop + } +} \ No newline at end of file _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
