blob: e9e278737752eff583bdb465bf5670e0e53b5c6d [file] [log] [blame]
Martin Brænne36f51822023-04-03 23:36:50 -07001// Part of the Crubit project, under the Apache License v2.0 with LLVM
2// Exceptions. See /LICENSE for license information.
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5// Tests for merging different nullability types.
6
Googler7f19b2b2023-05-01 09:44:57 -07007#include "nullability/test/check_diagnostics.h"
Martin Brænne36f51822023-04-03 23:36:50 -07008#include "third_party/llvm/llvm-project/third-party/unittest/googletest/include/gtest/gtest.h"
9
10namespace clang {
11namespace tidy {
12namespace nullability {
13namespace {
14
15TEST(PointerNullabilityTest, MergeNullAndNonNull) {
16 EXPECT_TRUE(checkDiagnostics(R"cc(
17 void target(int *_Nonnull y, bool b) {
18 int *x = nullptr;
19 *x; // [[unsafe]]
20 if (b) {
21 *x; // [[unsafe]]
22 x = y;
23 *x;
24 }
25 *x; // [[unsafe]]
26 if (b) {
27 *x;
28 } else {
29 *x; // [[unsafe]]
30 }
31 }
32 )cc"));
33}
34
35TEST(PointerNullabilityTest, MergeNullAndNullable) {
36 EXPECT_TRUE(checkDiagnostics(R"cc(
37 void target(int *_Nullable y, bool b) {
38 int *x = nullptr;
39 *x; // [[unsafe]]
40 if (b) {
41 *x; // [[unsafe]]
42 x = y;
43 *x; // [[unsafe]]
44 }
45 *x; // [[unsafe]]
46 if (b) {
47 *x; // [[unsafe]]
48 } else {
49 *x; // [[unsafe]]
50 }
51 }
52 )cc"));
53}
54
55TEST(PointerNullabilityTest, MergeNullAndUnknown) {
56 EXPECT_TRUE(checkDiagnostics(R"cc(
57 void target(int *y, bool b) {
58 int *x = nullptr;
59 *x; // [[unsafe]]
60 if (b) {
61 *x; // [[unsafe]]
62 x = y;
63 *x;
64 }
65 *x; // [[unsafe]]
66 if (b) {
67 *x;
68 } else {
69 *x; // [[unsafe]]
70 }
71 }
72 )cc"));
73}
74
75TEST(PointerNullabilityTest, MergeNonNullAndNull) {
76 EXPECT_TRUE(checkDiagnostics(R"cc(
77 void target(int *_Nonnull y, bool b) {
78 int *x = y;
79 *x;
80 if (b) {
81 *x;
82 x = nullptr;
83 *x; // [[unsafe]]
84 }
85 *x; // [[unsafe]]
86 if (b) {
87 *x; // [[unsafe]]
88 } else {
89 *x;
90 }
91 }
92 )cc"));
93}
94
95TEST(PointerNullabilityTest, MergeNonNullAndNonNull) {
96 EXPECT_TRUE(checkDiagnostics(R"cc(
97 void target(int *_Nonnull y, int *_Nonnull z, bool b) {
98 int *x = y;
99 *x;
100 if (b) {
101 *x;
102 x = z;
103 *x;
104 }
105 *x;
106 if (b) {
107 *x;
108 } else {
109 *x;
110 }
111 }
112 )cc"));
113}
114
115TEST(PointerNullabilityTest, MergeNonNullAndNullable) {
116 EXPECT_TRUE(checkDiagnostics(R"cc(
117 void target(int *_Nonnull y, int *_Nullable z, bool b) {
118 int *x = y;
119 *x;
120 if (b) {
121 *x;
122 x = z;
123 *x; // [[unsafe]]
124 }
125 *x; // [[unsafe]]
126 if (b) {
127 *x; // [[unsafe]]
128 } else {
129 *x;
130 }
131 }
132 )cc"));
133}
134
135TEST(PointerNullabilityTest, MergeNonNullAndUnknown) {
136 EXPECT_TRUE(checkDiagnostics(R"cc(
137 void target(int *_Nonnull y, int *z, bool b) {
138 int *x = y;
139 *x;
140 if (b) {
141 *x;
142 x = z;
143 *x;
144 }
145 *x;
146 if (b) {
147 *x;
148 } else {
149 *x;
150 }
151 }
152 )cc"));
153}
154
155TEST(PointerNullabilityTest, MergeNullableAndNull) {
156 EXPECT_TRUE(checkDiagnostics(R"cc(
157 void target(int *_Nullable y, bool b) {
158 int *x = y;
159 *x; // [[unsafe]]
160 if (b) {
161 *x; // [[unsafe]]
162 x = nullptr;
163 *x; // [[unsafe]]
164 }
165 *x; // [[unsafe]]
166 if (b) {
167 *x; // [[unsafe]]
168 } else {
169 *x; // [[unsafe]]
170 }
171 }
172 )cc"));
173}
174
175TEST(PointerNullabilityTest, MergeNullableAndNonNull) {
176 EXPECT_TRUE(checkDiagnostics(R"cc(
177 void target(int *_Nullable y, int *_Nonnull z, bool b) {
178 int *x = y;
179 *x; // [[unsafe]]
180 if (b) {
181 *x; // [[unsafe]]
182 x = z;
183 *x;
184 }
185 *x; // [[unsafe]]
186 if (b) {
187 *x;
188 } else {
189 *x; // [[unsafe]]
190 }
191 }
192 )cc"));
193}
194
195TEST(PointerNullabilityTest, MergeNullableAndNullable) {
196 EXPECT_TRUE(checkDiagnostics(R"cc(
197 void target(int *_Nullable y, int *_Nullable z, bool b) {
198 int *x = y;
199 *x; // [[unsafe]]
200 if (b) {
201 *x; // [[unsafe]]
202 x = z;
203 *x; // [[unsafe]]
204 }
205 *x; // [[unsafe]]
206 if (b) {
207 *x; // [[unsafe]]
208 } else {
209 *x; // [[unsafe]]
210 }
211 }
212 )cc"));
213}
214
215TEST(PointerNullabilityTest, MergeNullableAndUnknown) {
216 EXPECT_TRUE(checkDiagnostics(R"cc(
217 void target(int *_Nullable y, int *z, bool b) {
218 int *x = y;
219 *x; // [[unsafe]]
220 if (b) {
221 *x; // [[unsafe]]
222 x = z;
223 *x;
224 }
225 *x; // [[unsafe]]
226 if (b) {
227 *x;
228 } else {
229 *x; // [[unsafe]]
230 }
231 }
232 )cc"));
233}
234
235TEST(PointerNullabilityTest, MergeUnknownAndNull) {
236 EXPECT_TRUE(checkDiagnostics(R"cc(
237 void target(int *y, bool b) {
238 int *x = y;
239 *x;
240 if (b) {
241 *x;
242 x = nullptr;
243 *x; // [[unsafe]]
244 }
245 *x; // [[unsafe]]
246 if (b) {
247 *x; // [[unsafe]]
248 } else {
249 *x;
250 }
251 }
252 )cc"));
253}
254
255TEST(PointerNullabilityTest, MergeUnknownAndNonNull) {
256 EXPECT_TRUE(checkDiagnostics(R"cc(
257 void target(int *y, int *_Nonnull z, bool b) {
258 int *x = y;
259 *x;
260 if (b) {
261 *x;
262 x = z;
263 *x;
264 }
265 *x;
266 if (b) {
267 *x;
268 } else {
269 *x;
270 }
271 }
272 )cc"));
273}
274
275TEST(PointerNullabilityTest, MergeUnknownAndNullable) {
276 EXPECT_TRUE(checkDiagnostics(R"cc(
277 void target(int *y, int *_Nullable z, bool b) {
278 int *x = y;
279 *x;
280 if (b) {
281 *x;
282 x = z;
283 *x; // [[unsafe]]
284 }
285 *x; // [[unsafe]]
286 if (b) {
287 *x; // [[unsafe]]
288 } else {
289 *x;
290 }
291 }
292 )cc"));
293}
294
295TEST(PointerNullabilityTest, MergeUnknownAndUnknown) {
296 EXPECT_TRUE(checkDiagnostics(R"cc(
297 void target(int *y, int *z, bool b) {
298 int *x = y;
299 if (b) {
300 *x;
301 x = z;
302 *x;
303 }
304 *x;
305 if (b) {
306 *x;
307 } else {
308 *x;
309 }
310 }
311 )cc"));
312}
313
314} // namespace
315} // namespace nullability
316} // namespace tidy
317} // namespace clang