blob: 28c5ae1521357282d9c1a8cd25447d5dc23580cc [file] [log] [blame]
Alexander Kornienko1b7bf7a2015-08-19 22:21:37 +00001// RUN: $(dirname %s)/check_clang_tidy.sh %s modernize-use-nullptr %t -- \
2// RUN: -std=c++98 -Wno-non-literal-null-conversion
3// REQUIRES: shell
4
5const unsigned int g_null = 0;
6#define NULL 0
7
8void test_assignment() {
9 int *p1 = 0;
10 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr [modernize-use-nullptr]
11 // CHECK-FIXES: int *p1 = nullptr;
12 p1 = 0;
13 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use nullptr
14 // CHECK-FIXES: p1 = nullptr;
15
16 int *p2 = NULL;
17 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr
18 // CHECK-FIXES: int *p2 = nullptr;
19
20 p2 = p1;
21 // CHECK-FIXES: p2 = p1;
22
23 const int null = 0;
24 int *p3 = null;
25 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr
26 // CHECK-FIXES: int *p3 = nullptr;
27
28 p3 = NULL;
29 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use nullptr
30 // CHECK-FIXES: p3 = nullptr;
31
32 int *p4 = p3;
33 // CHECK-FIXES: int *p4 = p3;
34
35 p4 = null;
36 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use nullptr
37 // CHECK-FIXES: p4 = nullptr;
38
39 int i1 = 0;
40
41 int i2 = NULL;
42
43 int i3 = null;
44
45 int *p5, *p6, *p7;
46 p5 = p6 = p7 = NULL;
47 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr
48 // CHECK-FIXES: p5 = p6 = p7 = nullptr;
49}
50
51struct Foo {
52 Foo(int *p = NULL) : m_p1(p) {}
53 // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use nullptr
54 // CHECK-FIXES: Foo(int *p = nullptr) : m_p1(p) {}
55
56 void bar(int *p = 0) {}
57 // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: use nullptr
58 // CHECK-FIXES: void bar(int *p = nullptr) {}
59
60 void baz(int i = 0) {}
61
62 int *m_p1;
63 static int *m_p2;
64};
65
66int *Foo::m_p2 = NULL;
67// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr
68// CHECK-FIXES: int *Foo::m_p2 = nullptr;
69
70template <typename T>
71struct Bar {
72 Bar(T *p) : m_p(p) {
73 m_p = static_cast<T*>(NULL);
74 // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: use nullptr
75 // CHECK-FIXES: m_p = static_cast<T*>(nullptr);
76
77 m_p = static_cast<T*>(reinterpret_cast<int*>((void*)NULL));
78 // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: use nullptr
79 // CHECK-FIXES: m_p = static_cast<T*>(nullptr);
80
81 m_p = static_cast<T*>(p ? p : static_cast<void*>(g_null));
82 // CHECK-MESSAGES: :[[@LINE-1]]:54: warning: use nullptr
83 // CHECK-FIXES: m_p = static_cast<T*>(p ? p : static_cast<void*>(nullptr));
84
85 T *p2 = static_cast<T*>(reinterpret_cast<int*>((void*)NULL));
86 // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use nullptr
87 // CHECK-FIXES: T *p2 = static_cast<T*>(nullptr);
88
89 m_p = NULL;
90 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use nullptr
91 // CHECK-FIXES: m_p = nullptr;
92
93 int i = static_cast<int>(0.f);
94 T *i2 = static_cast<int>(0.f);
95 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr
96 // CHECK-FIXES: T *i2 = nullptr;
97 }
98
99 T *m_p;
100};
101
102struct Baz {
103 Baz() : i(0) {}
104 int i;
105};
106
107void test_cxx_cases() {
108 Foo f(g_null);
109 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use nullptr
110 // CHECK-FIXES: Foo f(nullptr);
111
112 f.bar(NULL);
113 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use nullptr
114 // CHECK-FIXES: f.bar(nullptr);
115
116 f.baz(g_null);
117
118 f.m_p1 = 0;
119 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use nullptr
120 // CHECK-FIXES: f.m_p1 = nullptr;
121
122 Bar<int> b(g_null);
123 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use nullptr
124 // CHECK-FIXES: Bar<int> b(nullptr);
125
126 Baz b2;
127 int Baz::*memptr(0);
128 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use nullptr
129 // CHECK-FIXES: int Baz::*memptr(nullptr);
130
131 memptr = 0;
132 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use nullptr
133 // CHECK-FIXES: memptr = nullptr;
134}
135
136void test_function_default_param1(void *p = 0);
137// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: use nullptr
138// CHECK-FIXES: void test_function_default_param1(void *p = nullptr);
139
140void test_function_default_param2(void *p = NULL);
141// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: use nullptr
142// CHECK-FIXES: void test_function_default_param2(void *p = nullptr);
143
144void test_function_default_param3(void *p = g_null);
145// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: use nullptr
146// CHECK-FIXES: void test_function_default_param3(void *p = nullptr);
147
148void test_function(int *p) {}
149
150void test_function_no_ptr_param(int i) {}
151
152void test_function_call() {
153 test_function(0);
154 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr
155 // CHECK-FIXES: test_function(nullptr);
156
157 test_function(NULL);
158 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr
159 // CHECK-FIXES: test_function(nullptr);
160
161 test_function(g_null);
162 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr
163 // CHECK-FIXES: test_function(nullptr);
164
165 test_function_no_ptr_param(0);
166}
167
168char *test_function_return1() {
169 return 0;
170 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr
171 // CHECK-FIXES: return nullptr;
172}
173
174void *test_function_return2() {
175 return NULL;
176 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr
177 // CHECK-FIXES: return nullptr;
178}
179
180long *test_function_return3() {
181 return g_null;
182 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr
183 // CHECK-FIXES: return nullptr;
184}
185
186int test_function_return4() {
187 return 0;
188}
189
190int test_function_return5() {
191 return NULL;
192}
193
194int test_function_return6() {
195 return g_null;
196}
197
198int *test_function_return_cast1() {
199 return(int)0;
200 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use nullptr
201 // CHECK-FIXES: return nullptr;
202}
203
204int *test_function_return_cast2() {
205#define RET return
206 RET(int)0;
207 // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use nullptr
208 // CHECK-FIXES: RET nullptr;
209#undef RET
210}
211
212// Test parentheses expressions resulting in a nullptr.
213int *test_parentheses_expression1() {
214 return(0);
215 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr
216 // CHECK-FIXES: return(nullptr);
217}
218
219int *test_parentheses_expression2() {
220 return(int(0.f));
221 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr
222 // CHECK-FIXES: return(nullptr);
223}
224
225int *test_nested_parentheses_expression() {
226 return((((0))));
227 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr
228 // CHECK-FIXES: return((((nullptr))));
229}
230
231void *test_parentheses_explicit_cast() {
232 return(static_cast<void*>(0));
233 // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use nullptr
234 // CHECK-FIXES: return(static_cast<void*>(nullptr));
235}
236
237void *test_parentheses_explicit_cast_sequence1() {
238 return(static_cast<void*>(static_cast<int*>((void*)NULL)));
239 // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use nullptr
240 // CHECK-FIXES: return(static_cast<void*>(nullptr));
241}
242
243void *test_parentheses_explicit_cast_sequence2() {
244 return(static_cast<void*>(reinterpret_cast<int*>((float*)int(0.f))));
245 // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use nullptr
246 // CHECK-FIXES: return(static_cast<void*>(nullptr));
247}
248
249// Test explicit cast expressions resulting in nullptr.
250struct Bam {
251 Bam(int *a) {}
252 Bam(float *a) {}
253 Bam operator=(int *a) { return Bam(a); }
254 Bam operator=(float *a) { return Bam(a); }
255};
256
257void ambiguous_function(int *a) {}
258void ambiguous_function(float *a) {}
259void const_ambiguous_function(const int *p) {}
260void const_ambiguous_function(const float *p) {}
261
262void test_explicit_cast_ambiguous1() {
263 ambiguous_function((int*)0);
264 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use nullptr
265 // CHECK-FIXES: ambiguous_function((int*)nullptr);
266}
267
268void test_explicit_cast_ambiguous2() {
269 ambiguous_function((int*)(0));
270 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use nullptr
271 // CHECK-FIXES: ambiguous_function((int*)nullptr);
272}
273
274void test_explicit_cast_ambiguous3() {
275 ambiguous_function(static_cast<int*>(reinterpret_cast<int*>((float*)0)));
276 // CHECK-MESSAGES: :[[@LINE-1]]:40: warning: use nullptr
277 // CHECK-FIXES: ambiguous_function(static_cast<int*>(nullptr));
278}
279
280Bam test_explicit_cast_ambiguous4() {
281 return(((int*)(0)));
282 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr
283 // CHECK-FIXES: return(((int*)nullptr));
284}
285
286void test_explicit_cast_ambiguous5() {
287 // Test for ambiguous overloaded constructors.
288 Bam k((int*)(0));
289 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use nullptr
290 // CHECK-FIXES: Bam k((int*)nullptr);
291
292 // Test for ambiguous overloaded operators.
293 k = (int*)0;
294 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr
295 // CHECK-FIXES: k = (int*)nullptr;
296}
297
298void test_const_pointers_abiguous() {
299 const_ambiguous_function((int*)0);
300 // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use nullptr
301 // CHECK-FIXES: const_ambiguous_function((int*)nullptr);
302}
303
304// Test where the implicit cast to null is surrounded by another implict cast
305// with possible explict casts in-between.
306void test_const_pointers() {
307 const int *const_p1 = 0;
308 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr
309 // CHECK-FIXES: const int *const_p1 = nullptr;
310 const int *const_p2 = NULL;
311 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr
312 // CHECK-FIXES: const int *const_p2 = nullptr;
313 const int *const_p3 = (int)0;
314 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr
315 // CHECK-FIXES: const int *const_p3 = nullptr;
316 const int *const_p4 = (int)0.0f;
317 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr
318 // CHECK-FIXES: const int *const_p4 = nullptr;
319 const int *const_p5 = (int*)0;
320 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: use nullptr
321 // CHECK-FIXES: const int *const_p5 = (int*)nullptr;
322 int *t;
323 const int *const_p6 = static_cast<int*>(t ? t : static_cast<int*>(0));
324 // CHECK-MESSAGES: :[[@LINE-1]]:69: warning: use nullptr
325 // CHECK-FIXES: const int *const_p6 = static_cast<int*>(t ? t : static_cast<int*>(nullptr));
326}
327
328
329// FIXME: currently, the check doesn't work as it should with templates.
330template<typename T>
331class A {
332 public:
333 A(T *p = NULL) {}
334
335 void f() {
336 Ptr = NULL;
337 }
338 T *Ptr;
339};
340
341template<typename T>
342T *f2(T *a = NULL) {
343 return a ? a : NULL;
344}