-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[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
HerrCai0907
merged 2 commits into
llvm:main
from
HerrCai0907:95596-clang-tidy-performance-inefficient-vector-operation-doesnt-warn-if-loop-has-unsigned-index
Jun 16, 2024
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…peration Fixes: llvm#95596 Check will warn if the loop var type is not same as var init expr type
@llvm/pr-subscribers-clang-tools-extra @llvm/pr-subscribers-clang-tidy Author: Congcong Cai (HerrCai0907) ChangesFixes: #95596 Full diff: https://github.com/llvm/llvm-project/pull/95667.diff 3 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp b/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp
index 20aea5a79fe9a..dc6e0cf9c7d12 100644
--- a/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp
@@ -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)))));
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 7092d0b6fdb02..3bdd735f7dcf7 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -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.
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 c28592f4d6368..50424920d72b7 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
@@ -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
\ No newline at end of file
|
5chmidti
approved these changes
Jun 15, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp
Outdated
Show resolved
Hide resolved
PiotrZSL
approved these changes
Jun 15, 2024
HerrCai0907
commented
Jun 15, 2024
clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp
Outdated
Show resolved
Hide resolved
…ient-vector-operation.cpp
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes: #95596
Avoid false negatives caused by different variable definition type and variable initial value type in loop initialization expression.