| 1 | // RUN: %check_clang_tidy %s performance-unnecessary-copy-initialization %t -- -config="{CheckOptions: {performance-unnecessary-copy-initialization.ExcludedContainerTypes: 'ns::ViewType$;::ConstInCorrectType$'}}" -- |
|---|---|
| 2 | |
| 3 | namespace ns { |
| 4 | template <typename T> |
| 5 | struct ViewType { |
| 6 | ViewType(const T &); |
| 7 | const T &view() const; |
| 8 | }; |
| 9 | } // namespace ns |
| 10 | |
| 11 | template <typename T> |
| 12 | struct ViewType { |
| 13 | ViewType(const T &); |
| 14 | const T &view() const; |
| 15 | }; |
| 16 | |
| 17 | struct ExpensiveToCopy { |
| 18 | ~ExpensiveToCopy(); |
| 19 | void constMethod() const; |
| 20 | }; |
| 21 | |
| 22 | struct ConstInCorrectType { |
| 23 | const ExpensiveToCopy &secretlyMutates() const; |
| 24 | const ExpensiveToCopy &operator[](int) const; |
| 25 | }; |
| 26 | |
| 27 | using NSVTE = ns::ViewType<ExpensiveToCopy>; |
| 28 | typedef ns::ViewType<ExpensiveToCopy> FewType; |
| 29 | |
| 30 | void positiveViewType() { |
| 31 | ExpensiveToCopy E; |
| 32 | ViewType<ExpensiveToCopy> V(E); |
| 33 | const auto O = V.view(); |
| 34 | // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'O' is copy-constructed |
| 35 | // CHECK-FIXES: const auto& O = V.view(); |
| 36 | O.constMethod(); |
| 37 | } |
| 38 | |
| 39 | void excludedViewTypeInNamespace() { |
| 40 | ExpensiveToCopy E; |
| 41 | ns::ViewType<ExpensiveToCopy> V(E); |
| 42 | const auto O = V.view(); |
| 43 | O.constMethod(); |
| 44 | } |
| 45 | |
| 46 | void excludedViewTypeAliased() { |
| 47 | ExpensiveToCopy E; |
| 48 | NSVTE V(E); |
| 49 | const auto O = V.view(); |
| 50 | O.constMethod(); |
| 51 | |
| 52 | FewType F(E); |
| 53 | const auto P = F.view(); |
| 54 | P.constMethod(); |
| 55 | } |
| 56 | |
| 57 | void excludedConstIncorrectType() { |
| 58 | ConstInCorrectType C; |
| 59 | const auto E = C.secretlyMutates(); |
| 60 | E.constMethod(); |
| 61 | } |
| 62 | |
| 63 | void excludedConstIncorrectTypeOperator() { |
| 64 | ConstInCorrectType C; |
| 65 | const auto E = C[42]; |
| 66 | E.constMethod(); |
| 67 | } |
| 68 | |
| 69 | void excludedConstIncorrectTypeAsPointer(ConstInCorrectType *C) { |
| 70 | const auto E = C->secretlyMutates(); |
| 71 | E.constMethod(); |
| 72 | } |
| 73 | |
| 74 | void excludedConstIncorrectTypeAsPointerOperator(ConstInCorrectType *C) { |
| 75 | const auto E = (*C)[42]; |
| 76 | E.constMethod(); |
| 77 | } |
| 78 | |
| 79 | void excludedConstIncorrectTypeAsReference(const ConstInCorrectType &C) { |
| 80 | const auto E = C.secretlyMutates(); |
| 81 | E.constMethod(); |
| 82 | } |
| 83 | |
| 84 | void excludedConstIncorrectTypeAsReferenceOperator(const ConstInCorrectType &C) { |
| 85 | const auto E = C[42]; |
| 86 | E.constMethod(); |
| 87 | } |
| 88 |
