Skip to content

[clang-tidy] fix false negatives for performance-inefficient-vector-operation #95667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ void InefficientVectorOperationCheck::addMatcher(
onImplicitObjectArgument(declRefExpr(to(TargetVarDecl))))
.bind(AppendCallName);
const auto AppendCall = expr(ignoringImplicit(AppendCallExpr));
const auto LoopVarInit =
declStmt(hasSingleDecl(varDecl(hasInitializer(integerLiteral(equals(0))))
.bind(LoopInitVarName)));
const auto LoopVarInit = declStmt(hasSingleDecl(
varDecl(hasInitializer(ignoringParenImpCasts(integerLiteral(equals(0)))))
.bind(LoopInitVarName)));
const auto RefersToLoopVar = ignoringParenImpCasts(
declRefExpr(to(varDecl(equalsBoundNode(LoopInitVarName)))));

Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,11 @@ Changes in existing checks
- Improved :doc:`modernize-use-using <clang-tidy/checks/modernize/use-using>`
check by adding support for detection of typedefs declared on function level.

- Improved :doc:`performance-inefficient-vector-operation
<clang-tidy/checks/performance/inefficient-vector-operation>` fixing false
negatives caused by different variable definition type and variable initial
value type in loop initialization expression.

- Improved :doc:`performance-move-const-arg
<clang-tidy/checks/performance/move-const-arg>` check by ignoring
``std::move()`` calls when their target is used as an rvalue.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading