1// RUN: %check_clang_tidy %s bugprone-unused-raii %t -- -- -fno-delayed-template-parsing
2
3struct Foo {
4 Foo();
5 Foo(int);
6 Foo(int, int);
7 ~Foo();
8};
9
10struct Bar {
11 Bar();
12};
13
14struct FooBar {
15 FooBar();
16 Foo f;
17};
18
19template <typename T>
20void qux() {
21 T(42);
22}
23
24template <typename T>
25struct TFoo {
26 TFoo(T);
27 ~TFoo();
28};
29
30Foo f();
31
32struct Ctor {
33 Ctor(int);
34 Ctor() {
35 Ctor(0); // TODO: warn here.
36 }
37};
38
39template <typename T>
40void templ() {
41 T();
42}
43
44template <typename T>
45void neverInstantiated() {
46 T();
47}
48
49struct CtorDefaultArg {
50 CtorDefaultArg(int i = 0);
51 ~CtorDefaultArg();
52};
53
54template <typename T>
55struct TCtorDefaultArg {
56 TCtorDefaultArg(T i = 0);
57 ~TCtorDefaultArg();
58};
59
60struct CtorTwoDefaultArg {
61 CtorTwoDefaultArg(int i = 0, bool b = false);
62 ~CtorTwoDefaultArg();
63};
64
65template <typename T>
66void 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
85template <typename T>
86void 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
107void 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

source code of clang-tools-extra/test/clang-tidy/checkers/bugprone/unused-raii.cpp