xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-raii.cpp (revision 89a1d03e2b379e325daa5249411e414bbd995b5e)
1 // RUN: %check_clang_tidy %s bugprone-unused-raii %t -- -- -fno-delayed-template-parsing
2 
3 struct Foo {
4   Foo();
5   Foo(int);
6   Foo(int, int);
7   ~Foo();
8 };
9 
10 struct Bar {
11   Bar();
12 };
13 
14 struct FooBar {
15   FooBar();
16   Foo f;
17 };
18 
19 template <typename T>
qux()20 void qux() {
21   T(42);
22 }
23 
24 template <typename T>
25 struct TFoo {
26   TFoo(T);
27   ~TFoo();
28 };
29 
30 Foo f();
31 
32 struct Ctor {
33   Ctor(int);
CtorCtor34   Ctor() {
35     Ctor(0); // TODO: warn here.
36   }
37 };
38 
39 template <typename T>
templ()40 void templ() {
41   T();
42 }
43 
44 template <typename T>
neverInstantiated()45 void neverInstantiated() {
46   T();
47 }
48 
49 struct CtorDefaultArg {
50   CtorDefaultArg(int i = 0);
51   ~CtorDefaultArg();
52 };
53 
54 template <typename T>
55 struct TCtorDefaultArg {
56   TCtorDefaultArg(T i = 0);
57   ~TCtorDefaultArg();
58 };
59 
60 struct CtorTwoDefaultArg {
61   CtorTwoDefaultArg(int i = 0, bool b = false);
62   ~CtorTwoDefaultArg();
63 };
64 
65 template <typename T>
templatetest()66 void templatetest() {
67   TCtorDefaultArg<T>();
68   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
69   // CHECK-FIXES: TCtorDefaultArg<T> give_me_a_name;
70   TCtorDefaultArg<T>{};
71   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
72   // CHECK-FIXES: TCtorDefaultArg<T> give_me_a_name;
73 
74   TCtorDefaultArg<T>(T{});
75   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
76   // CHECK-FIXES: TCtorDefaultArg<T> give_me_a_name(T{});
77   TCtorDefaultArg<T>{T{}};
78   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
79   // CHECK-FIXES: TCtorDefaultArg<T> give_me_a_name{T{}};
80 
81   int i = 0;
82   (void)i;
83 }
84 
85 template <typename T>
aliastest()86 void aliastest() {
87   using X = Foo;
88   using Y = X;
89   using Z = Y;
90   Z(42);
91   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
92   // CHECK-FIXES: Z give_me_a_name(42);
93 
94   typedef Z ZT;
95   ZT(42, 13);
96   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
97   // CHECK-FIXES: ZT give_me_a_name(42, 13);
98 
99   using TT = TCtorDefaultArg<T>;
100   TT(42);
101   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
102   // CHECK-FIXES: TT give_me_a_name(42);
103 
104   (void)0;
105 }
106 
test()107 void test() {
108   Foo(42);
109 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
110 // CHECK-FIXES: Foo give_me_a_name(42);
111   Foo(23, 42);
112 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
113 // CHECK-FIXES: Foo give_me_a_name(23, 42);
114   Foo();
115 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
116 // CHECK-FIXES: Foo give_me_a_name;
117   TFoo<int>(23);
118 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
119 // CHECK-FIXES: TFoo<int> give_me_a_name(23);
120 
121   FooBar();
122 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
123 // CHECK-FIXES: FooBar give_me_a_name;
124 
125   Foo{42};
126   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
127   // CHECK-FIXES: Foo give_me_a_name{42};
128   FooBar{};
129   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
130   // CHECK-FIXES: FooBar give_me_a_name;
131 
132   CtorDefaultArg();
133   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
134   // CHECK-FIXES: CtorDefaultArg give_me_a_name;
135 
136   CtorTwoDefaultArg();
137   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
138   // CHECK-FIXES: CtorTwoDefaultArg give_me_a_name;
139 
140   TCtorDefaultArg<int>();
141   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
142   // CHECK-FIXES: TCtorDefaultArg<int> give_me_a_name;
143 
144   TCtorDefaultArg<int>{};
145   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
146   // CHECK-FIXES: TCtorDefaultArg<int> give_me_a_name;
147 
148   templ<FooBar>();
149   templ<Bar>();
150 
151   Bar();
152   f();
153   qux<Foo>();
154 
155 #define M Foo();
156   M
157 
158   {
159     Foo();
160   }
161   Foo();
162 }
163