[clang-tools-extra] [clang-tidy] Fix false negative in performance-inefficient-vector-ope… (PR #179484)
Luka Aladashvili via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 3 21:57:37 PST 2026
https://github.com/llukito updated https://github.com/llvm/llvm-project/pull/179484
>From e0d915f68f6994ee4987f230020a290e49833038 Mon Sep 17 00:00:00 2001
From: Luka Aladashvili <115102487+llukito at users.noreply.github.com>
Date: Tue, 3 Feb 2026 15:32:33 +0000
Subject: [PATCH 1/3] [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
>From 1a3901e38f427b8907af900cb0b1ac3bbc2007cd Mon Sep 17 00:00:00 2001
From: Luka Aladashvili <115102487+llukito at users.noreply.github.com>
Date: Tue, 3 Feb 2026 23:40:56 +0400
Subject: [PATCH 2/3] Fix test expectations for multi-statement loops
---
.../performance/inefficient-vector-operation.cpp | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
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 631354605e915..e198c531e74e0 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
@@ -321,9 +321,10 @@ void f(std::vector<int>& t) {
}
{
FooProto foo;
- // CHECK-FIXES-NOT: foo.mutable_x()->Reserve(5);
+ // CHECK-FIXES: foo.mutable_x()->Reserve(5);
for (int i = 0; i < 5; i++) {
foo.add_x(i);
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'add_x' is called inside a loop
foo.add_x(i);
}
}
@@ -339,9 +340,10 @@ void f(std::vector<int>& t) {
FooProto foo;
BarProto bar;
bar.mutable_x();
- // CHECK-FIXES-NOT: foo.mutable_x()->Reserve(5);
+ // CHECK-FIXES: foo.mutable_x()->Reserve(5);
for (int i = 0; i < 5; i++) {
foo.add_x();
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'add_x' is called inside a loop
bar.add_x();
}
}
@@ -436,4 +438,5 @@ void multiple_statements_in_loop() {
v2.push_back(i);
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: 'push_back' is called inside a loop
}
-}
\ No newline at end of file
+}
+
>From 165ec6a5bfbb64bb33a9d7e61443a00f70e56270 Mon Sep 17 00:00:00 2001
From: Luka Aladashvili <115102487+llukito at users.noreply.github.com>
Date: Wed, 4 Feb 2026 09:57:27 +0400
Subject: [PATCH 3/3] [clang-tidy] Fix code formatting in
InefficientVectorOperationCheck.cpp
---
.../performance/InefficientVectorOperationCheck.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp b/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp
index 8f8bbbfd10937..b6de5ac8730fd 100644
--- a/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp
@@ -113,8 +113,8 @@ void InefficientVectorOperationCheck::addMatcher(
// Matchers for the loop whose body contains a push_back/emplace_back calling
// statement.
- const auto HasInterestingLoopBody = hasBody(
- anyOf(compoundStmt(has(AppendCall)), AppendCall));
+ const auto HasInterestingLoopBody =
+ hasBody(anyOf(compoundStmt(has(AppendCall)), AppendCall));
const auto InInterestingCompoundStmt =
hasParent(compoundStmt(has(TargetVarDefStmt)).bind(LoopParentName));
More information about the cfe-commits
mailing list