[clang-tools-extra] [clang-tidy] fix false negatives for performance-inefficient-vector-operation (PR #95667)

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Sat Jun 15 15:14:18 PDT 2024


================
@@ -387,3 +387,38 @@ void foo(const StructWithFieldContainer &Src) {
     B.push_back(Number);
   }
 }
+
+namespace gh95596 {
+
+void f(std::vector<int>& t) {
+  {
+    std::vector<int> gh95596_0;
+    // CHECK-FIXES: gh95596_0.reserve(10);
+    for (unsigned i = 0; i < 10; ++i)
+      gh95596_0.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
+  }
+  {
+    std::vector<int> gh95596_1;
+    // CHECK-FIXES: gh95596_1.reserve(10);
+    for (int i = 0U; i < 10; ++i)
+      gh95596_1.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
+  }
+  {
+    std::vector<int> gh95596_2;
+    // CHECK-FIXES: gh95596_2.reserve(10);
+    for (unsigned i = 0U; i < 10; ++i)
+      gh95596_2.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
+  }
+  {
+    std::vector<int> gh95596_3;
+    // CHECK-FIXES: gh95596_3.reserve(10U);
+    for (int i = 0; i < 10U; ++i)
+      gh95596_3.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
+  }
+}
+
+} // namespace gh95596
----------------
HerrCai0907 wrote:

```suggestion
} // namespace gh95596

```

https://github.com/llvm/llvm-project/pull/95667


More information about the cfe-commits mailing list