Skip to content

Commit 2d72c48

Browse files
committed
[clang-tidy] performance-unnecessary-copy-initialization: Consider static functions
Static member functions can be considered the same way as free functions are, so do that.
1 parent c3276a9 commit 2d72c48

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,16 @@ AST_MATCHER_FUNCTION_P(StatementMatcher,
104104
hasArgument(0, hasType(ReceiverType)))));
105105
}
106106

107+
AST_MATCHER(CXXMethodDecl, isStatic) { return Node.isStatic(); }
108+
107109
AST_MATCHER_FUNCTION(StatementMatcher, isConstRefReturningFunctionCall) {
108110
// Only allow initialization of a const reference from a free function if it
109111
// has no arguments. Otherwise it could return an alias to one of its
110112
// arguments and the arguments need to be checked for const use as well.
111113
return callExpr(callee(functionDecl(returns(hasCanonicalType(
112114
matchers::isReferenceToConst())))
113115
.bind(FunctionDeclId)),
114-
argumentCountIs(0), unless(callee(cxxMethodDecl())))
116+
argumentCountIs(0), unless(callee(cxxMethodDecl(unless(isStatic())))))
115117
.bind(InitFunctionCallId);
116118
}
117119

@@ -232,7 +234,7 @@ UnnecessaryCopyInitialization::UnnecessaryCopyInitialization(
232234
Options.get("ExcludedContainerTypes", ""))) {}
233235

234236
void UnnecessaryCopyInitialization::registerMatchers(MatchFinder *Finder) {
235-
auto LocalVarCopiedFrom = [this](const internal::Matcher<Expr> &CopyCtorArg) {
237+
auto LocalVarCopiedFrom = [this](const ast_matchers::internal::Matcher<Expr> &CopyCtorArg) {
236238
return compoundStmt(
237239
forEachDescendant(
238240
declStmt(

clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ struct ExpensiveToCopyType {
2828
template <typename A>
2929
const A &templatedAccessor() const;
3030
operator int() const; // Implicit conversion to int.
31+
32+
static const ExpensiveToCopyType &instance();
3133
};
3234

3335
template <typename T>
@@ -100,6 +102,28 @@ void PositiveFunctionCall() {
100102
VarCopyConstructed.constMethod();
101103
}
102104

105+
void PositiveStaticMethodCall() {
106+
const auto AutoAssigned = ExpensiveToCopyType::instance();
107+
// 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]
108+
// CHECK-FIXES: const auto& AutoAssigned = ExpensiveToCopyType::instance();
109+
AutoAssigned.constMethod();
110+
111+
const auto AutoCopyConstructed(ExpensiveToCopyType::instance());
112+
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed'
113+
// CHECK-FIXES: const auto& AutoCopyConstructed(ExpensiveToCopyType::instance());
114+
AutoCopyConstructed.constMethod();
115+
116+
const ExpensiveToCopyType VarAssigned = ExpensiveToCopyType::instance();
117+
// CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned'
118+
// CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = ExpensiveToCopyType::instance();
119+
VarAssigned.constMethod();
120+
121+
const ExpensiveToCopyType VarCopyConstructed(ExpensiveToCopyType::instance());
122+
// CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed'
123+
// CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(ExpensiveToCopyType::instance());
124+
VarCopyConstructed.constMethod();
125+
}
126+
103127
void PositiveMethodCallConstReferenceParam(const ExpensiveToCopyType &Obj) {
104128
const auto AutoAssigned = Obj.reference();
105129
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'

0 commit comments

Comments
 (0)