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 -- -- -fno-delayed-template-parsing
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,47 +58,13 @@ int *Foo::m_p2 = NULL;
7058// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr
7159// CHECK-FIXES: int *Foo::m_p2 = nullptr;
7260
73- template <typename T>
74- struct Bar {
75- Bar (T *p) : m_p(p) {
76- m_p = static_cast <T*>(NULL );
77- // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: use nullptr
78- // CHECK-FIXES: m_p = static_cast<T*>(nullptr);
79-
80- m_p = static_cast <T*>(reinterpret_cast <int *>((void *)NULL ));
81- // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: use nullptr
82- // CHECK-FIXES: m_p = static_cast<T*>(nullptr);
83-
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-
88- T *p2 = static_cast <T*>(reinterpret_cast <int *>((void *)NULL ));
89- // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use nullptr
90- // CHECK-FIXES: T *p2 = static_cast<T*>(nullptr);
91-
92- m_p = NULL ;
93- // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use nullptr
94- // CHECK-FIXES: m_p = nullptr;
95-
96- int i = static_cast <int >(0 .f );
97- T *i2 = static_cast <int >(0 .f );
98- // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr
99- // CHECK-FIXES: T *i2 = nullptr;
100- }
101-
102- T *m_p;
103- };
104-
10561struct Baz {
10662 Baz () : i(0 ) {}
10763 int i;
10864};
10965
11066void test_cxx_cases () {
111- Foo f (g_null);
112- // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use nullptr
113- // CHECK-FIXES: Foo f(nullptr);
67+ Foo f;
11468
11569 f.bar (NULL );
11670 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use nullptr
@@ -122,10 +76,6 @@ void test_cxx_cases() {
12276 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use nullptr
12377 // CHECK-FIXES: f.m_p1 = nullptr;
12478
125- Bar<int > b (g_null);
126- // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use nullptr
127- // CHECK-FIXES: Bar<int> b(nullptr);
128-
12979 Baz b2;
13080 int Baz::*memptr (0 );
13181 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use nullptr
@@ -144,10 +94,6 @@ void test_function_default_param2(void *p = NULL);
14494// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: use nullptr
14595// CHECK-FIXES: void test_function_default_param2(void *p = nullptr);
14696
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-
15197void test_function (int *p) {}
15298
15399void test_function_no_ptr_param (int i) {}
@@ -161,10 +107,6 @@ void test_function_call() {
161107 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr
162108 // CHECK-FIXES: test_function(nullptr);
163109
164- test_function (g_null);
165- // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr
166- // CHECK-FIXES: test_function(nullptr);
167-
168110 test_function_no_ptr_param (0 );
169111}
170112
@@ -180,51 +122,33 @@ void *test_function_return2() {
180122 // CHECK-FIXES: return nullptr;
181123}
182124
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 () {
125+ int test_function_return3 () {
190126 return 0 ;
191127}
192128
193- int test_function_return5 () {
129+ int test_function_return4 () {
194130 return NULL ;
195131}
196132
197- int test_function_return6 () {
133+ int test_function_return5 () {
198134 return g_null;
199135}
200136
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 () {
137+ int *test_function_return_cast () {
208138#define RET return
209- RET ( int ) 0 ;
210- // CHECK-MESSAGES: :[[@LINE-1]]:6 : warning: use nullptr
139+ RET 0 ;
140+ // CHECK-MESSAGES: :[[@LINE-1]]:7 : warning: use nullptr
211141 // CHECK-FIXES: RET nullptr;
212142#undef RET
213143}
214144
215145// Test parentheses expressions resulting in a nullptr.
216- int *test_parentheses_expression1 () {
146+ int *test_parentheses_expression () {
217147 return (0 );
218148 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr
219149 // CHECK-FIXES: return(nullptr);
220150}
221151
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-
228152int *test_nested_parentheses_expression () {
229153 return ((((0 ))));
230154 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr
@@ -244,7 +168,7 @@ void *test_parentheses_explicit_cast_sequence1() {
244168}
245169
246170void *test_parentheses_explicit_cast_sequence2 () {
247- return (static_cast <void *>(reinterpret_cast <int *>((float *)int ( 0 . f ))));
171+ return (static_cast <void *>(reinterpret_cast <int *>((float *)( 0 ))));
248172 // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use nullptr
249173 // CHECK-FIXES: return(static_cast<void*>(nullptr));
250174}
@@ -313,19 +237,13 @@ void test_const_pointers() {
313237 const int *const_p2 = NULL ;
314238 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr
315239 // 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 ;
240+ const int *const_p3 = (int *)0 ;
323241 // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: use nullptr
324- // CHECK-FIXES: const int *const_p5 = (int*)nullptr;
242+ // CHECK-FIXES: const int *const_p3 = (int*)nullptr;
325243 int *t;
326- const int *const_p6 = static_cast <int *>(t ? t : static_cast <int *>(0 ));
244+ const int *const_p4 = static_cast <int *>(t ? t : static_cast <int *>(0 ));
327245 // CHECK-MESSAGES: :[[@LINE-1]]:69: warning: use nullptr
328- // CHECK-FIXES: const int *const_p6 = static_cast<int*>(t ? t : static_cast<int*>(nullptr));
246+ // CHECK-FIXES: const int *const_p4 = static_cast<int*>(t ? t : static_cast<int*>(nullptr));
329247}
330248
331249void test_nested_implicit_cast_expr () {
@@ -348,7 +266,21 @@ void test_nested_implicit_cast_expr() {
348266template <typename T>
349267class A {
350268 public:
351- A (T *p = NULL ) {}
269+ A (T *p = NULL ) {
270+ Ptr = static_cast <T*>(NULL );
271+
272+ Ptr = static_cast <T*>(reinterpret_cast <int *>((void *)NULL ));
273+ // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: use nullptr
274+ // CHECK-FIXES: Ptr = static_cast<T*>(nullptr);
275+ // FIXME: a better fix-it is: Ptr = nullptr;
276+
277+ T *p2 = static_cast <T*>(reinterpret_cast <int *>((void *)NULL ));
278+ // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use nullptr
279+ // CHECK-FIXES: T *p2 = static_cast<T*>(nullptr);
280+ // FIXME: a better fix-it is: T *p2 = nullptr;
281+
282+ Ptr = NULL ;
283+ }
352284
353285 void f () {
354286 Ptr = NULL ;
0 commit comments