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