Skip to content

Commit dcbe0d4

Browse files
authored
[clang-tidy] fix false negatives for performance-inefficient-vector-operation (#95667)
Fixes: #95596 Avoid false negatives caused by different variable definition type and variable initial value type in loop initialization expression.
1 parent 85a7bba commit dcbe0d4

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ void InefficientVectorOperationCheck::addMatcher(
105105
onImplicitObjectArgument(declRefExpr(to(TargetVarDecl))))
106106
.bind(AppendCallName);
107107
const auto AppendCall = expr(ignoringImplicit(AppendCallExpr));
108-
const auto LoopVarInit =
109-
declStmt(hasSingleDecl(varDecl(hasInitializer(integerLiteral(equals(0))))
110-
.bind(LoopInitVarName)));
108+
const auto LoopVarInit = declStmt(hasSingleDecl(
109+
varDecl(hasInitializer(ignoringParenImpCasts(integerLiteral(equals(0)))))
110+
.bind(LoopInitVarName)));
111111
const auto RefersToLoopVar = ignoringParenImpCasts(
112112
declRefExpr(to(varDecl(equalsBoundNode(LoopInitVarName)))));
113113

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,11 @@ Changes in existing checks
387387
- Improved :doc:`modernize-use-using <clang-tidy/checks/modernize/use-using>`
388388
check by adding support for detection of typedefs declared on function level.
389389

390+
- Improved :doc:`performance-inefficient-vector-operation
391+
<clang-tidy/checks/performance/inefficient-vector-operation>` fixing false
392+
negatives caused by different variable definition type and variable initial
393+
value type in loop initialization expression.
394+
390395
- Improved :doc:`performance-move-const-arg
391396
<clang-tidy/checks/performance/move-const-arg>` check by ignoring
392397
``std::move()`` calls when their target is used as an rvalue.

clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,38 @@ void foo(const StructWithFieldContainer &Src) {
387387
B.push_back(Number);
388388
}
389389
}
390+
391+
namespace gh95596 {
392+
393+
void f(std::vector<int>& t) {
394+
{
395+
std::vector<int> gh95596_0;
396+
// CHECK-FIXES: gh95596_0.reserve(10);
397+
for (unsigned i = 0; i < 10; ++i)
398+
gh95596_0.push_back(i);
399+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside a loop; consider pre-allocating the container capacity before the loop
400+
}
401+
{
402+
std::vector<int> gh95596_1;
403+
// CHECK-FIXES: gh95596_1.reserve(10);
404+
for (int i = 0U; i < 10; ++i)
405+
gh95596_1.push_back(i);
406+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside a loop; consider pre-allocating the container capacity before the loop
407+
}
408+
{
409+
std::vector<int> gh95596_2;
410+
// CHECK-FIXES: gh95596_2.reserve(10);
411+
for (unsigned i = 0U; i < 10; ++i)
412+
gh95596_2.push_back(i);
413+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside a loop; consider pre-allocating the container capacity before the loop
414+
}
415+
{
416+
std::vector<int> gh95596_3;
417+
// CHECK-FIXES: gh95596_3.reserve(10U);
418+
for (int i = 0; i < 10U; ++i)
419+
gh95596_3.push_back(i);
420+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside a loop; consider pre-allocating the container capacity before the loop
421+
}
422+
}
423+
424+
} // namespace gh95596

0 commit comments

Comments
 (0)