-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[clang-tidy] performance-unnecessary-copy-initialization: Consider static functions #119974
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
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang-tidy @llvm/pr-subscribers-clang-tools-extra Author: Barnabás Pőcze (pobrn) ChangesStatic member functions can be considered the same way as free functions are, so do that.
Full diff: https://github.com/llvm/llvm-project/pull/119974.diff 2 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp b/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp
index 034894c11bf2c0..778d1d00250315 100644
--- a/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp
+++ b/clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp
@@ -104,6 +104,10 @@ AST_MATCHER_FUNCTION_P(StatementMatcher,
hasArgument(0, hasType(ReceiverType)))));
}
+namespace {
+AST_MATCHER(CXXMethodDecl, isStatic) { return Node.isStatic(); }
+} // namespace
+
AST_MATCHER_FUNCTION(StatementMatcher, isConstRefReturningFunctionCall) {
// Only allow initialization of a const reference from a free function if it
// has no arguments. Otherwise it could return an alias to one of its
@@ -111,7 +115,7 @@ AST_MATCHER_FUNCTION(StatementMatcher, isConstRefReturningFunctionCall) {
return callExpr(callee(functionDecl(returns(hasCanonicalType(
matchers::isReferenceToConst())))
.bind(FunctionDeclId)),
- argumentCountIs(0), unless(callee(cxxMethodDecl())))
+ argumentCountIs(0), unless(callee(cxxMethodDecl(unless(isStatic())))))
.bind(InitFunctionCallId);
}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp
index d02bb98cf583cb..b5325776f54c61 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp
@@ -28,6 +28,8 @@ struct ExpensiveToCopyType {
template <typename A>
const A &templatedAccessor() const;
operator int() const; // Implicit conversion to int.
+
+ static const ExpensiveToCopyType &instance();
};
template <typename T>
@@ -100,6 +102,28 @@ void PositiveFunctionCall() {
VarCopyConstructed.constMethod();
}
+void PositiveStaticMethodCall() {
+ const auto AutoAssigned = ExpensiveToCopyType::instance();
+ // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned' is copy-constructed from a const reference; consider making it a const reference [performance-unnecessary-copy-initialization]
+ // CHECK-FIXES: const auto& AutoAssigned = ExpensiveToCopyType::instance();
+ AutoAssigned.constMethod();
+
+ const auto AutoCopyConstructed(ExpensiveToCopyType::instance());
+ // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed'
+ // CHECK-FIXES: const auto& AutoCopyConstructed(ExpensiveToCopyType::instance());
+ AutoCopyConstructed.constMethod();
+
+ const ExpensiveToCopyType VarAssigned = ExpensiveToCopyType::instance();
+ // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned'
+ // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = ExpensiveToCopyType::instance();
+ VarAssigned.constMethod();
+
+ const ExpensiveToCopyType VarCopyConstructed(ExpensiveToCopyType::instance());
+ // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed'
+ // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(ExpensiveToCopyType::instance());
+ VarCopyConstructed.constMethod();
+}
+
void PositiveMethodCallConstReferenceParam(const ExpensiveToCopyType &Obj) {
const auto AutoAssigned = Obj.reference();
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
|
6a160c9
to
2d72c48
Compare
Please mention changes in Release Notes. |
fcacca2
to
e9cd529
Compare
Sorry, I hope it is fixed now. |
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.
Not sure how many more tests I should add, since this uses the same code paths as free functions.
I think the amount of tests you've added are enough
This is the 5th instance of the
isStatic
matcher in clang-tidy.
Good point, thanks. I've checked to see if they could be replaced with isStaticStorageClass
, but that won't work due to #120027, and because CXXMethodDecl::isStatic
does more than just check the storage class. You can just keep it as-is.
clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp
Outdated
Show resolved
Hide resolved
…atic functions Static member functions can be considered the same way as free functions are, so do that.
e9cd529
to
ee2b0c2
Compare
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
@pobrn Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/4/builds/4559 Here is the relevant piece of the build log for the reference
|
Static member functions can be considered the same way as free functions are, so do that.
isStatic
matcher inclang-tidy
.