1// RUN: %check_clang_tidy %s zircon-temporary-objects %t -- \
2// RUN: -config="{CheckOptions: {zircon-temporary-objects.Names: 'Foo;NS::Bar'}}" \
3// RUN: -header-filter=.*
4
5// Should flag instances of Foo, NS::Bar.
6
7class Foo {
8public:
9 Foo() = default;
10 Foo(int Val) : Val(Val){};
11
12private:
13 int Val;
14};
15
16namespace NS {
17
18class Bar {
19public:
20 Bar() = default;
21 Bar(int Val) : Val(Val){};
22
23private:
24 int Val;
25};
26
27} // namespace NS
28
29class Bar {
30public:
31 Bar() = default;
32 Bar(int Val) : Val(Val){};
33
34private:
35 int Val;
36};
37
38int func(Foo F) { return 1; };
39
40int main() {
41 Foo F;
42 Foo *F2 = new Foo();
43 new Foo();
44 Foo();
45 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: creating a temporary object of type 'Foo' is prohibited
46 Foo F3 = Foo();
47 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: creating a temporary object of type 'Foo' is prohibited
48
49 Bar();
50 NS::Bar();
51// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: creating a temporary object of type 'NS::Bar' is prohibited
52
53 int A = func(F: Foo());
54 // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: creating a temporary object of type 'Foo' is prohibited
55
56 Foo F4(0);
57 Foo *F5 = new Foo(0);
58 new Foo(0);
59 Foo(0);
60 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: creating a temporary object of type 'Foo' is prohibited
61 Foo F6 = Foo(0);
62 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: creating a temporary object of type 'Foo' is prohibited
63
64 Bar(0);
65 NS::Bar(0);
66// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: creating a temporary object of type 'NS::Bar' is prohibited
67
68 int B = func(F: Foo(0));
69 // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: creating a temporary object of type 'Foo' is prohibited
70}
71
72namespace NS {
73
74void f() {
75 Bar();
76// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: creating a temporary object of type 'NS::Bar' is prohibited
77 Bar(0);
78// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: creating a temporary object of type 'NS::Bar' is prohibited
79}
80
81} // namespace NS
82
83template <typename Ty>
84Ty make_ty() { return Ty(); }
85// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: creating a temporary object of type 'Foo' is prohibited
86// CHECK-MESSAGES: :[[@LINE-2]]:23: warning: creating a temporary object of type 'NS::Bar' is prohibited
87
88void ty_func() {
89 make_ty<Bar>();
90 make_ty<NS::Bar>();
91 make_ty<Foo>();
92}
93
94// Inheriting the disallowed class does not trigger the check.
95
96class Bingo : NS::Bar {}; // Not explicitly disallowed
97
98void f2() {
99 Bingo();
100}
101
102template <typename Ty>
103class Quux : Ty {};
104
105void f3() {
106 Quux<NS::Bar>();
107 Quux<Bar>();
108}
109

source code of clang-tools-extra/test/clang-tidy/checkers/zircon/temporary-objects.cpp