Debian Codegraph @

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

raw
1 // RUN: %check_clang_tidy -std=c++14-or-later %s performance-unnecessary-value-param %t
2
3 struct ExpensiveToCopyType {
4 virtual ~ExpensiveToCopyType();
5 };
6
7 template <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
13 void instantiatedWithExpensiveValue() {
14 templateWithNonTemplatizedParameter(
15 ExpensiveToCopyType(), ExpensiveToCopyType());
16 templateWithNonTemplatizedParameter(ExpensiveToCopyType(), 5);
17 }
18
19 template <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
24 void instantiatedWithCheapValue() {
25 templateWithNonTemplatizedParameterCheapTemplate(ExpensiveToCopyType(), 5);
26 }
27
28 template <typename T> void nonInstantiatedTemplateWithConstValue(const T S) {}
29 template <typename T> void nonInstantiatedTemplateWithNonConstValue(T S) {}
30
31 template <typename T> void instantiatedTemplateSpecialization(T NoSpecS) {}
32 template <> 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.
37 template <>
38 void 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
46 void instantiatedTemplateSpecialization() {
47 instantiatedTemplateSpecialization(ExpensiveToCopyType());
48 }
49
50 template <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
55 void instantiatedConstValue() {
56 instantiatedTemplateWithConstValue(ExpensiveToCopyType());
57 }
58
59 template <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
64 void instantiatedNonConstValue() {
65 instantiatedTemplateWithNonConstValue(ExpensiveToCopyType());
66 }
67
68 void 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
76 void 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
84 void 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
92 void 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 }