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
7
2
8
3
const unsigned int g_null = 0 ;
9
4
#define NULL 0
@@ -23,26 +18,19 @@ void test_assignment() {
23
18
p2 = p1;
24
19
// CHECK-FIXES: p2 = p1;
25
20
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;
31
22
p3 = NULL ;
32
23
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use nullptr
33
24
// CHECK-FIXES: p3 = nullptr;
34
25
35
26
int *p4 = p3;
36
27
// CHECK-FIXES: int *p4 = p3;
37
28
38
- p4 = null;
39
- // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use nullptr
40
- // CHECK-FIXES: p4 = nullptr;
41
-
42
29
int i1 = 0 ;
43
30
44
31
int i2 = NULL ;
45
32
33
+ const int null = 0 ;
46
34
int i3 = null;
47
35
48
36
int *p5, *p6, *p7;
@@ -70,47 +58,13 @@ int *Foo::m_p2 = NULL;
70
58
// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr
71
59
// CHECK-FIXES: int *Foo::m_p2 = nullptr;
72
60
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
-
105
61
struct Baz {
106
62
Baz () : i(0 ) {}
107
63
int i;
108
64
};
109
65
110
66
void 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;
114
68
115
69
f.bar (NULL );
116
70
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use nullptr
@@ -122,10 +76,6 @@ void test_cxx_cases() {
122
76
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use nullptr
123
77
// CHECK-FIXES: f.m_p1 = nullptr;
124
78
125
- Bar<int > b (g_null);
126
- // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use nullptr
127
- // CHECK-FIXES: Bar<int> b(nullptr);
128
-
129
79
Baz b2;
130
80
int Baz::*memptr (0 );
131
81
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use nullptr
@@ -144,10 +94,6 @@ void test_function_default_param2(void *p = NULL);
144
94
// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: use nullptr
145
95
// CHECK-FIXES: void test_function_default_param2(void *p = nullptr);
146
96
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
-
151
97
void test_function (int *p) {}
152
98
153
99
void test_function_no_ptr_param (int i) {}
@@ -161,10 +107,6 @@ void test_function_call() {
161
107
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr
162
108
// CHECK-FIXES: test_function(nullptr);
163
109
164
- test_function (g_null);
165
- // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr
166
- // CHECK-FIXES: test_function(nullptr);
167
-
168
110
test_function_no_ptr_param (0 );
169
111
}
170
112
@@ -180,51 +122,33 @@ void *test_function_return2() {
180
122
// CHECK-FIXES: return nullptr;
181
123
}
182
124
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 () {
190
126
return 0 ;
191
127
}
192
128
193
- int test_function_return5 () {
129
+ int test_function_return4 () {
194
130
return NULL ;
195
131
}
196
132
197
- int test_function_return6 () {
133
+ int test_function_return5 () {
198
134
return g_null;
199
135
}
200
136
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 () {
208
138
#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
211
141
// CHECK-FIXES: RET nullptr;
212
142
#undef RET
213
143
}
214
144
215
145
// Test parentheses expressions resulting in a nullptr.
216
- int *test_parentheses_expression1 () {
146
+ int *test_parentheses_expression () {
217
147
return (0 );
218
148
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr
219
149
// CHECK-FIXES: return(nullptr);
220
150
}
221
151
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
-
228
152
int *test_nested_parentheses_expression () {
229
153
return ((((0 ))));
230
154
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr
@@ -244,7 +168,7 @@ void *test_parentheses_explicit_cast_sequence1() {
244
168
}
245
169
246
170
void *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 ))));
248
172
// CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use nullptr
249
173
// CHECK-FIXES: return(static_cast<void*>(nullptr));
250
174
}
@@ -313,19 +237,13 @@ void test_const_pointers() {
313
237
const int *const_p2 = NULL ;
314
238
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr
315
239
// 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 ;
323
241
// 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;
325
243
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 ));
327
245
// 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));
329
247
}
330
248
331
249
void test_nested_implicit_cast_expr () {
@@ -348,7 +266,21 @@ void test_nested_implicit_cast_expr() {
348
266
template <typename T>
349
267
class A {
350
268
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
+ }
352
284
353
285
void f () {
354
286
Ptr = NULL ;
0 commit comments