130 lines · cpp
1// RUN: %check_clang_tidy -std=c++14-or-later %s performance-unnecessary-value-param %t2 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 ExpensiveToCopyType(), ExpensiveToCopyType());16 templateWithNonTemplatizedParameter(ExpensiveToCopyType(), 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(ExpensiveToCopyType(), 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 main34// template and other specializations. Such specializations may be35// 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& NoSpecS42 // CHECK-FIXES-NOT: const int& SpecSInt43 // CHECK-FIXES-NOT: const ExpensiveToCopyType& SpecSExpensiveToCopy44}45 46void instantiatedTemplateSpecialization() {47 instantiatedTemplateSpecialization(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(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(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 100template <typename... Args>101void ParameterPack(Args... args) {102 // CHECK-MESSAGES: [[@LINE-1]]:28: warning: the parameter 'args' of type 'ExpensiveToCopyType'103 // CHECK-FIXES: void ParameterPack(const Args&... args) {104}105 106template <typename... Args>107void ParameterPackConst(Args const... args) {108 // CHECK-MESSAGES: [[@LINE-1]]:39: warning: the const qualified parameter 'args' of type 'const ExpensiveToCopyType'109 // CHECK-FIXES: void ParameterPackConst(Args const&... args) {110}111 112template <typename... Args>113void ParameterPackWithParams(const ExpensiveToCopyType E1, ExpensiveToCopyType E2, Args... args) {114 // CHECK-MESSAGES: [[@LINE-1]]:56: warning: the const qualified parameter 'E1'115 // CHECK-MESSAGES: [[@LINE-2]]:80: warning: the parameter 'E2'116 // CHECK-MESSAGES: [[@LINE-3]]:92: warning: the parameter 'args'117 // CHECK-FIXES: void ParameterPackWithParams(const ExpensiveToCopyType& E1, const ExpensiveToCopyType& E2, const Args&... args) {118}119 120template <typename... Args>121void PackWithNonExpensive(int x, Args... args) {}122 123void instantiatedParameterPack() {124 ExpensiveToCopyType E;125 ParameterPack(E);126 ParameterPackConst(E);127 ParameterPackWithParams(E, E, E);128 PackWithNonExpensive(5, 5);129}130