1// RUN: %check_clang_tidy -std=c++14-or-later %s performance-unnecessary-value-param %t
2
3struct ExpensiveToCopyType {
4 virtual ~ExpensiveToCopyType();
5};
6
7template <typename T> void templateWithNonTemplatizedParameter(const ExpensiveToCopyType S, T V) {
8 // CHECK-MESSAGES: [[@LINE-1]]:90: warning: the const qualified parameter 'S'
9 // CHECK-MESSAGES: [[@LINE-2]]:95: warning: the parameter 'V'
10 // CHECK-FIXES: template <typename T> void templateWithNonTemplatizedParameter(const ExpensiveToCopyType& S, const T& V) {
11}
12
13void instantiatedWithExpensiveValue() {
14 templateWithNonTemplatizedParameter(
15 S: ExpensiveToCopyType(), V: ExpensiveToCopyType());
16 templateWithNonTemplatizedParameter(S: ExpensiveToCopyType(), V: 5);
17}
18
19template <typename T> void templateWithNonTemplatizedParameterCheapTemplate(const ExpensiveToCopyType S, T V) {
20 // CHECK-MESSAGES: [[@LINE-1]]:103: warning: the const qualified parameter 'S'
21 // CHECK-FIXES: template <typename T> void templateWithNonTemplatizedParameterCheapTemplate(const ExpensiveToCopyType& S, T V) {
22}
23
24void instantiatedWithCheapValue() {
25 templateWithNonTemplatizedParameterCheapTemplate(S: ExpensiveToCopyType(), V: 5);
26}
27
28template <typename T> void nonInstantiatedTemplateWithConstValue(const T S) {}
29template <typename T> void nonInstantiatedTemplateWithNonConstValue(T S) {}
30
31template <typename T> void instantiatedTemplateSpecialization(T NoSpecS) {}
32template <> void instantiatedTemplateSpecialization<int>(int SpecSInt) {}
33// Updating template specialization would also require to update the main
34// template and other specializations. Such specializations may be
35// spreaded across different translation units.
36// For that reason we only issue a warning, but do not propose fixes.
37template <>
38void instantiatedTemplateSpecialization<ExpensiveToCopyType>(
39 ExpensiveToCopyType SpecSExpensiveToCopy) {
40 // CHECK-MESSAGES: [[@LINE-1]]:25: warning: the parameter 'SpecSExpensiveToCopy'
41 // CHECK-FIXES-NOT: const T& NoSpecS
42 // CHECK-FIXES-NOT: const int& SpecSInt
43 // CHECK-FIXES-NOT: const ExpensiveToCopyType& SpecSExpensiveToCopy
44}
45
46void instantiatedTemplateSpecialization() {
47 instantiatedTemplateSpecialization(SpecSExpensiveToCopy: ExpensiveToCopyType());
48}
49
50template <typename T> void instantiatedTemplateWithConstValue(const T S) {
51 // CHECK-MESSAGES: [[@LINE-1]]:71: warning: the const qualified parameter 'S'
52 // CHECK-FIXES: template <typename T> void instantiatedTemplateWithConstValue(const T& S) {
53}
54
55void instantiatedConstValue() {
56 instantiatedTemplateWithConstValue(S: ExpensiveToCopyType());
57}
58
59template <typename T> void instantiatedTemplateWithNonConstValue(T S) {
60 // CHECK-MESSAGES: [[@LINE-1]]:68: warning: the parameter 'S'
61 // CHECK-FIXES: template <typename T> void instantiatedTemplateWithNonConstValue(const T& S) {
62}
63
64void instantiatedNonConstValue() {
65 instantiatedTemplateWithNonConstValue(S: ExpensiveToCopyType());
66}
67
68void lambdaConstValue() {
69 auto fn = [](const ExpensiveToCopyType S) {
70 // CHECK-MESSAGES: [[@LINE-1]]:42: warning: the const qualified parameter 'S'
71 // CHECK-FIXES: auto fn = [](const ExpensiveToCopyType& S) {
72 };
73 fn(ExpensiveToCopyType());
74}
75
76void lambdaNonConstValue() {
77 auto fn = [](ExpensiveToCopyType S) {
78 // CHECK-MESSAGES: [[@LINE-1]]:36: warning: the parameter 'S'
79 // CHECK-FIXES: auto fn = [](const ExpensiveToCopyType& S) {
80 };
81 fn(ExpensiveToCopyType());
82}
83
84void lambdaConstAutoValue() {
85 auto fn = [](const auto S) {
86 // CHECK-MESSAGES: [[@LINE-1]]:27: warning: the const qualified parameter 'S'
87 // CHECK-FIXES: auto fn = [](const auto& S) {
88 };
89 fn(ExpensiveToCopyType());
90}
91
92void lambdaNonConstAutoValue() {
93 auto fn = [](auto S) {
94 // CHECK-MESSAGES: [[@LINE-1]]:21: warning: the parameter 'S'
95 // CHECK-FIXES: auto fn = [](const auto& S) {
96 };
97 fn(ExpensiveToCopyType());
98}
99

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-value-param-templates.cpp