brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.9 KiB · 42d7699 Raw
183 lines · cpp
1// RUN: %check_clang_tidy %s performance-unnecessary-value-param %t -- -- -fdelayed-template-parsing2 3struct ExpensiveToCopyType {4  const ExpensiveToCopyType & constReference() const {5    return *this;6  }7  void nonConstMethod();8  virtual ~ExpensiveToCopyType();9};10 11void mutate(ExpensiveToCopyType &);12void mutate(ExpensiveToCopyType *);13void useAsConstReference(const ExpensiveToCopyType &);14void useByValue(ExpensiveToCopyType);15 16// This class simulates std::pair<>. It is trivially copy constructible17// and trivially destructible, but not trivially copy assignable.18class SomewhatTrivial {19 public:20  SomewhatTrivial();21  SomewhatTrivial(const SomewhatTrivial&) = default;22  ~SomewhatTrivial() = default;23  SomewhatTrivial& operator=(const SomewhatTrivial&);24};25 26void positiveExpensiveConstValue(const ExpensiveToCopyType Obj);27// CHECK-FIXES: void positiveExpensiveConstValue(const ExpensiveToCopyType& Obj);28void positiveExpensiveConstValue(const ExpensiveToCopyType Obj) {29  // CHECK-MESSAGES: [[@LINE-1]]:60: warning: the const qualified parameter 'Obj' of type 'const ExpensiveToCopyType' is copied for each invocation; consider making it a reference [performance-unnecessary-value-param]30  // CHECK-FIXES: void positiveExpensiveConstValue(const ExpensiveToCopyType& Obj) {31}32 33void positiveExpensiveValue(ExpensiveToCopyType Obj);34// CHECK-FIXES: void positiveExpensiveValue(const ExpensiveToCopyType& Obj);35void positiveExpensiveValue(ExpensiveToCopyType Obj) {36  // CHECK-MESSAGES: [[@LINE-1]]:49: warning: the parameter 'Obj' of type 'ExpensiveToCopyType' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]37  // CHECK-FIXES: void positiveExpensiveValue(const ExpensiveToCopyType& Obj) {38  Obj.constReference();39  useAsConstReference(Obj);40  auto Copy = Obj;41  useByValue(Obj);42}43 44void positiveWithComment(const ExpensiveToCopyType /* important */ S);45// CHECK-FIXES: void positiveWithComment(const ExpensiveToCopyType& /* important */ S);46void positiveWithComment(const ExpensiveToCopyType /* important */ S) {47  // CHECK-MESSAGES: [[@LINE-1]]:68: warning: the const qualified48  // CHECK-FIXES: void positiveWithComment(const ExpensiveToCopyType& /* important */ S) {49}50 51void positiveUnnamedParam(const ExpensiveToCopyType) {52  // CHECK-MESSAGES: [[@LINE-1]]:52: warning: the const qualified parameter #153  // CHECK-FIXES: void positiveUnnamedParam(const ExpensiveToCopyType&) {54}55 56void positiveAndNegative(const ExpensiveToCopyType ConstCopy, const ExpensiveToCopyType& ConstRef, ExpensiveToCopyType Copy);57// CHECK-FIXES: void positiveAndNegative(const ExpensiveToCopyType& ConstCopy, const ExpensiveToCopyType& ConstRef, const ExpensiveToCopyType& Copy);58void positiveAndNegative(const ExpensiveToCopyType ConstCopy, const ExpensiveToCopyType& ConstRef, ExpensiveToCopyType Copy) {59  // CHECK-MESSAGES: [[@LINE-1]]:52: warning: the const qualified parameter 'ConstCopy'60  // CHECK-MESSAGES: [[@LINE-2]]:120: warning: the parameter 'Copy'61  // CHECK-FIXES: void positiveAndNegative(const ExpensiveToCopyType& ConstCopy, const ExpensiveToCopyType& ConstRef, const ExpensiveToCopyType& Copy) {62}63 64struct PositiveConstValueConstructor {65  PositiveConstValueConstructor(const ExpensiveToCopyType ConstCopy) {}66  // CHECK-MESSAGES: [[@LINE-1]]:59: warning: the const qualified parameter 'ConstCopy'67  // CHECK-FIXES: PositiveConstValueConstructor(const ExpensiveToCopyType& ConstCopy) {}68};69 70template <typename T> void templateWithNonTemplatizedParameter(const ExpensiveToCopyType S, T V) {71  // CHECK-MESSAGES: [[@LINE-1]]:90: warning: the const qualified parameter 'S'72  // CHECK-MESSAGES: [[@LINE-2]]:95: warning: the parameter 'V'73  // CHECK-FIXES: template <typename T> void templateWithNonTemplatizedParameter(const ExpensiveToCopyType& S, const T& V) {74}75 76void instantiated() {77  templateWithNonTemplatizedParameter(ExpensiveToCopyType(), ExpensiveToCopyType());78  templateWithNonTemplatizedParameter(ExpensiveToCopyType(), 5);79}80 81template <typename T> void negativeTemplateType(const T V) {82}83 84void negativeArray(const ExpensiveToCopyType[]) {85}86 87void negativePointer(ExpensiveToCopyType* Obj) {88}89 90void negativeConstPointer(const ExpensiveToCopyType* Obj) {91}92 93void negativeConstReference(const ExpensiveToCopyType& Obj) {94}95 96void negativeReference(ExpensiveToCopyType& Obj) {97}98 99void negativeUniversalReference(ExpensiveToCopyType&& Obj) {100}101 102void negativeSomewhatTrivialConstValue(const SomewhatTrivial Somewhat) {103}104 105void negativeSomewhatTrivialValue(SomewhatTrivial Somewhat) {106}107 108void negativeConstBuiltIn(const int I) {109}110 111void negativeValueBuiltIn(int I) {112}113 114void negativeValueIsMutatedByReference(ExpensiveToCopyType Obj) {115  mutate(Obj);116}117 118void negativeValueIsMutatatedByPointer(ExpensiveToCopyType Obj) {119  mutate(&Obj);120}121 122void negativeValueIsReassigned(ExpensiveToCopyType Obj) {123  Obj = ExpensiveToCopyType();124}125 126void negativeValueNonConstMethodIsCalled(ExpensiveToCopyType Obj) {127  Obj.nonConstMethod();128}129 130struct PositiveValueUnusedConstructor {131  PositiveValueUnusedConstructor(ExpensiveToCopyType Copy) {}132  // CHECK-MESSAGES: [[@LINE-1]]:54: warning: the parameter 'Copy'133  // CHECK-FIXES: PositiveValueUnusedConstructor(const ExpensiveToCopyType& Copy) {}134};135 136struct PositiveValueCopiedConstructor {137  PositiveValueCopiedConstructor(ExpensiveToCopyType Copy) : Field(Copy) {}138  // CHECK-MESSAGES: [[@LINE-1]]:54: warning: the parameter 'Copy'139  // CHECK-FIXES: PositiveValueCopiedConstructor(const ExpensiveToCopyType& Copy) : Field(Copy) {}140  ExpensiveToCopyType Field;141};142 143template <typename T>144struct Container {145  typedef const T & const_reference;146};147 148void NegativeTypedefParam(const Container<ExpensiveToCopyType>::const_reference Param) {149}150 151#define UNNECESSARY_VALUE_PARAM_IN_MACRO_BODY()         \152  void inMacro(const ExpensiveToCopyType T) {           \153  }                                                     \154// Ensure fix is not applied.155// CHECK-FIXES: void inMacro(const ExpensiveToCopyType T) { {{\\}}156 157UNNECESSARY_VALUE_PARAM_IN_MACRO_BODY()158// CHECK-MESSAGES: [[@LINE-1]]:1: warning: the const qualified parameter 'T'159 160#define UNNECESSARY_VALUE_PARAM_IN_MACRO_ARGUMENT(ARGUMENT)     \161  ARGUMENT162 163UNNECESSARY_VALUE_PARAM_IN_MACRO_ARGUMENT(void inMacroArgument(const ExpensiveToCopyType InMacroArg) {})164// CHECK-MESSAGES: [[@LINE-1]]:90: warning: the const qualified parameter 'InMacroArg'165// CHECK-FIXES: UNNECESSARY_VALUE_PARAM_IN_MACRO_ARGUMENT(void inMacroArgument(const ExpensiveToCopyType InMacroArg) {})166 167struct VirtualMethod {168  virtual ~VirtualMethod() {}169  virtual void handle(ExpensiveToCopyType T) const = 0;170};171 172struct NegativeOverriddenMethod : public VirtualMethod {173  void handle(ExpensiveToCopyType Overridden) const {174    // CHECK-FIXES: void handle(ExpensiveToCopyType Overridden) const {175  }176};177 178struct NegativeDeletedMethod {179  ~NegativeDeletedMethod() {}180  NegativeDeletedMethod& operator=(NegativeDeletedMethod N) = delete;181  // CHECK-FIXES: NegativeDeletedMethod& operator=(NegativeDeletedMethod N) = delete;182};183