Skip to content

Commit 6a160c9

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 6a160c9

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,18 @@ AST_MATCHER_FUNCTION_P(StatementMatcher,
104104
hasArgument(0, hasType(ReceiverType)))));
105105
}
106106

107+
namespace {
108+
AST_MATCHER(CXXMethodDecl, isStatic) { return Node.isStatic(); }
109+
} // namespace
110+
107111
AST_MATCHER_FUNCTION(StatementMatcher, isConstRefReturningFunctionCall) {
108112
// Only allow initialization of a const reference from a free function if it
109113
// has no arguments. Otherwise it could return an alias to one of its
110114
// arguments and the arguments need to be checked for const use as well.
111115
return callExpr(callee(functionDecl(returns(hasCanonicalType(
112116
matchers::isReferenceToConst())))
113117
.bind(FunctionDeclId)),
114-
argumentCountIs(0), unless(callee(cxxMethodDecl())))
118+
argumentCountIs(0), unless(callee(cxxMethodDecl(unless(isStatic())))))
115119
.bind(InitFunctionCallId);
116120
}
117121

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)