llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tools-extra

Author: Luka Aladashvili (llukito)

<details>
<summary>Changes</summary>

### 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

---
Full diff: https://github.com/llvm/llvm-project/pull/179484.diff


2 Files Affected:

- (modified) 
clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp 
(+2-2) 
- (modified) 
clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp
 (+13) 


``````````diff
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

``````````

</details>


https://github.com/llvm/llvm-project/pull/179484
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to