1// RUN: %check_clang_tidy %s performance-unnecessary-copy-initialization %t -- -config="{CheckOptions: {performance-unnecessary-copy-initialization.ExcludedContainerTypes: 'ns::ViewType$;::ConstInCorrectType$'}}" --
2
3namespace ns {
4template <typename T>
5struct ViewType {
6 ViewType(const T &);
7 const T &view() const;
8};
9} // namespace ns
10
11template <typename T>
12struct ViewType {
13 ViewType(const T &);
14 const T &view() const;
15};
16
17struct ExpensiveToCopy {
18 ~ExpensiveToCopy();
19 void constMethod() const;
20};
21
22struct ConstInCorrectType {
23 const ExpensiveToCopy &secretlyMutates() const;
24 const ExpensiveToCopy &operator[](int) const;
25};
26
27using NSVTE = ns::ViewType<ExpensiveToCopy>;
28typedef ns::ViewType<ExpensiveToCopy> FewType;
29
30void 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
39void excludedViewTypeInNamespace() {
40 ExpensiveToCopy E;
41 ns::ViewType<ExpensiveToCopy> V(E);
42 const auto O = V.view();
43 O.constMethod();
44}
45
46void 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
57void excludedConstIncorrectType() {
58 ConstInCorrectType C;
59 const auto E = C.secretlyMutates();
60 E.constMethod();
61}
62
63void excludedConstIncorrectTypeOperator() {
64 ConstInCorrectType C;
65 const auto E = C[42];
66 E.constMethod();
67}
68
69void excludedConstIncorrectTypeAsPointer(ConstInCorrectType *C) {
70 const auto E = C->secretlyMutates();
71 E.constMethod();
72}
73
74void excludedConstIncorrectTypeAsPointerOperator(ConstInCorrectType *C) {
75 const auto E = (*C)[42];
76 E.constMethod();
77}
78
79void excludedConstIncorrectTypeAsReference(const ConstInCorrectType &C) {
80 const auto E = C.secretlyMutates();
81 E.constMethod();
82}
83
84void excludedConstIncorrectTypeAsReferenceOperator(const ConstInCorrectType &C) {
85 const auto E = C[42];
86 E.constMethod();
87}
88

source code of clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization-excluded-container-types.cpp