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
test()85 void test() {
86 Foo(42);
87 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
88 // CHECK-FIXES: Foo give_me_a_name(42);
89 Foo(23, 42);
90 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
91 // CHECK-FIXES: Foo give_me_a_name(23, 42);
92 Foo();
93 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
94 // CHECK-FIXES: Foo give_me_a_name;
95 TFoo<int>(23);
96 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
97 // CHECK-FIXES: TFoo<int> give_me_a_name(23);
98
99 FooBar();
100 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
101 // CHECK-FIXES: FooBar give_me_a_name;
102
103 Foo{42};
104 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
105 // CHECK-FIXES: Foo give_me_a_name{42};
106 FooBar{};
107 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
108 // CHECK-FIXES: FooBar give_me_a_name;
109
110 CtorDefaultArg();
111 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
112 // CHECK-FIXES: CtorDefaultArg give_me_a_name;
113
114 CtorTwoDefaultArg();
115 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
116 // CHECK-FIXES: CtorTwoDefaultArg give_me_a_name;
117
118 TCtorDefaultArg<int>();
119 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
120 // CHECK-FIXES: TCtorDefaultArg<int> give_me_a_name;
121
122 TCtorDefaultArg<int>{};
123 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: object destroyed immediately after creation; did you mean to name the object?
124 // CHECK-FIXES: TCtorDefaultArg<int> give_me_a_name;
125
126 templ<FooBar>();
127 templ<Bar>();
128
129 Bar();
130 f();
131 qux<Foo>();
132
133 #define M Foo();
134 M
135
136 {
137 Foo();
138 }
139 Foo();
140 }
141