1- // RUN: %check_clang_tidy -std=c++98 %s modernize-use-nullptr %t -- -- -Wno-non-literal-null-conversion
2- //
3- // Some parts of the test (e.g. assignment of `const int` to `int *`) fail in
4- // C++11, so we need to run the test in C++98 mode.
5- //
6- // FIXME: Make the test work in all language modes.
1+ // RUN: %check_clang_tidy %s modernize-use-nullptr %t -- -- -Wno-non-literal-null-conversion
72
83const unsigned int g_null = 0 ;
94#define NULL 0
@@ -23,26 +18,19 @@ void test_assignment() {
2318 p2 = p1;
2419 // CHECK-FIXES: p2 = p1;
2520
26- const int null = 0 ;
27- int *p3 = null;
28- // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr
29- // CHECK-FIXES: int *p3 = nullptr;
30-
21+ int *p3;
3122 p3 = NULL ;
3223 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use nullptr
3324 // CHECK-FIXES: p3 = nullptr;
3425
3526 int *p4 = p3;
3627 // CHECK-FIXES: int *p4 = p3;
3728
38- p4 = null;
39- // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use nullptr
40- // CHECK-FIXES: p4 = nullptr;
41-
4229 int i1 = 0 ;
4330
4431 int i2 = NULL ;
4532
33+ const int null = 0 ;
4634 int i3 = null;
4735
4836 int *p5, *p6, *p7;
@@ -70,33 +58,30 @@ int *Foo::m_p2 = NULL;
7058// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr
7159// CHECK-FIXES: int *Foo::m_p2 = nullptr;
7260
61+ // FIXME: all these DISABLED-* cases should trigger the warning.
7362template <typename T>
7463struct Bar {
7564 Bar (T *p) : m_p(p) {
7665 m_p = static_cast <T*>(NULL );
77- // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: use nullptr
78- // CHECK-FIXES: m_p = static_cast<T*>(nullptr);
66+ // DISABLED- CHECK-MESSAGES: :[[@LINE-1]]:27: warning: use nullptr
67+ // DISABLED- CHECK-FIXES: m_p = static_cast<T*>(nullptr);
7968
8069 m_p = static_cast <T*>(reinterpret_cast <int *>((void *)NULL ));
8170 // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: use nullptr
8271 // CHECK-FIXES: m_p = static_cast<T*>(nullptr);
8372
84- m_p = static_cast <T*>(p ? p : static_cast <void *>(g_null));
85- // CHECK-MESSAGES: :[[@LINE-1]]:54: warning: use nullptr
86- // CHECK-FIXES: m_p = static_cast<T*>(p ? p : static_cast<void*>(nullptr));
87-
8873 T *p2 = static_cast <T*>(reinterpret_cast <int *>((void *)NULL ));
8974 // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use nullptr
9075 // CHECK-FIXES: T *p2 = static_cast<T*>(nullptr);
9176
9277 m_p = NULL ;
93- // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use nullptr
94- // CHECK-FIXES: m_p = nullptr;
78+ // DISABLED- CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use nullptr
79+ // DISABLED- CHECK-FIXES: m_p = nullptr;
9580
9681 int i = static_cast <int >(0 .f );
9782 T *i2 = static_cast <int >(0 .f );
98- // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr
99- // CHECK-FIXES: T *i2 = nullptr;
83+ // DISABLED- CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr
84+ // DISABLED- CHECK-FIXES: T *i2 = nullptr;
10085 }
10186
10287 T *m_p;
@@ -108,9 +93,7 @@ struct Baz {
10893};
10994
11095void test_cxx_cases () {
111- Foo f (g_null);
112- // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use nullptr
113- // CHECK-FIXES: Foo f(nullptr);
96+ Foo f;
11497
11598 f.bar (NULL );
11699 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use nullptr
@@ -122,10 +105,6 @@ void test_cxx_cases() {
122105 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use nullptr
123106 // CHECK-FIXES: f.m_p1 = nullptr;
124107
125- Bar<int > b (g_null);
126- // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use nullptr
127- // CHECK-FIXES: Bar<int> b(nullptr);
128-
129108 Baz b2;
130109 int Baz::*memptr (0 );
131110 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use nullptr
@@ -144,10 +123,6 @@ void test_function_default_param2(void *p = NULL);
144123// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: use nullptr
145124// CHECK-FIXES: void test_function_default_param2(void *p = nullptr);
146125
147- void test_function_default_param3 (void *p = g_null);
148- // CHECK-MESSAGES: :[[@LINE-1]]:45: warning: use nullptr
149- // CHECK-FIXES: void test_function_default_param3(void *p = nullptr);
150-
151126void test_function (int *p) {}
152127
153128void test_function_no_ptr_param (int i) {}
@@ -161,10 +136,6 @@ void test_function_call() {
161136 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr
162137 // CHECK-FIXES: test_function(nullptr);
163138
164- test_function (g_null);
165- // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr
166- // CHECK-FIXES: test_function(nullptr);
167-
168139 test_function_no_ptr_param (0 );
169140}
170141
@@ -180,51 +151,33 @@ void *test_function_return2() {
180151 // CHECK-FIXES: return nullptr;
181152}
182153
183- long *test_function_return3 () {
184- return g_null;
185- // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr
186- // CHECK-FIXES: return nullptr;
187- }
188-
189- int test_function_return4 () {
154+ int test_function_return3 () {
190155 return 0 ;
191156}
192157
193- int test_function_return5 () {
158+ int test_function_return4 () {
194159 return NULL ;
195160}
196161
197- int test_function_return6 () {
162+ int test_function_return5 () {
198163 return g_null;
199164}
200165
201- int *test_function_return_cast1 () {
202- return (int )0 ;
203- // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use nullptr
204- // CHECK-FIXES: return nullptr;
205- }
206-
207- int *test_function_return_cast2 () {
166+ int *test_function_return_cast () {
208167#define RET return
209- RET ( int ) 0 ;
210- // CHECK-MESSAGES: :[[@LINE-1]]:6 : warning: use nullptr
168+ RET 0 ;
169+ // CHECK-MESSAGES: :[[@LINE-1]]:7 : warning: use nullptr
211170 // CHECK-FIXES: RET nullptr;
212171#undef RET
213172}
214173
215174// Test parentheses expressions resulting in a nullptr.
216- int *test_parentheses_expression1 () {
175+ int *test_parentheses_expression () {
217176 return (0 );
218177 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr
219178 // CHECK-FIXES: return(nullptr);
220179}
221180
222- int *test_parentheses_expression2 () {
223- return (int (0 .f ));
224- // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr
225- // CHECK-FIXES: return(nullptr);
226- }
227-
228181int *test_nested_parentheses_expression () {
229182 return ((((0 ))));
230183 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr
@@ -243,10 +196,11 @@ void *test_parentheses_explicit_cast_sequence1() {
243196 // CHECK-FIXES: return(static_cast<void*>(nullptr));
244197}
245198
199+ // FIXME: this case should trigger the warning.
246200void *test_parentheses_explicit_cast_sequence2 () {
247201 return (static_cast <void *>(reinterpret_cast <int *>((float *)int (0 .f ))));
248- // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use nullptr
249- // CHECK-FIXES: return(static_cast<void*>(nullptr));
202+ // DISABLED- CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use nullptr
203+ // DISABLED- CHECK-FIXES: return(static_cast<void*>(nullptr));
250204}
251205
252206// Test explicit cast expressions resulting in nullptr.
@@ -313,19 +267,13 @@ void test_const_pointers() {
313267 const int *const_p2 = NULL ;
314268 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr
315269 // CHECK-FIXES: const int *const_p2 = nullptr;
316- const int *const_p3 = (int )0 ;
317- // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr
318- // CHECK-FIXES: const int *const_p3 = nullptr;
319- const int *const_p4 = (int )0 .0f ;
320- // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr
321- // CHECK-FIXES: const int *const_p4 = nullptr;
322- const int *const_p5 = (int *)0 ;
270+ const int *const_p3 = (int *)0 ;
323271 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: use nullptr
324- // CHECK-FIXES: const int *const_p5 = (int*)nullptr;
272+ // CHECK-FIXES: const int *const_p3 = (int*)nullptr;
325273 int *t;
326- const int *const_p6 = static_cast <int *>(t ? t : static_cast <int *>(0 ));
274+ const int *const_p4 = static_cast <int *>(t ? t : static_cast <int *>(0 ));
327275 // CHECK-MESSAGES: :[[@LINE-1]]:69: warning: use nullptr
328- // CHECK-FIXES: const int *const_p6 = static_cast<int*>(t ? t : static_cast<int*>(nullptr));
276+ // CHECK-FIXES: const int *const_p4 = static_cast<int*>(t ? t : static_cast<int*>(nullptr));
329277}
330278
331279void test_nested_implicit_cast_expr () {
0 commit comments