Debian Codegraph @

clang/unittests/Analysis/FlowSensitive/UncheckedStatusOrAccessModelTestFixture.cpp

raw
1 //===- UncheckedStatusOrAccessModelTestFixture.cpp ------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "UncheckedStatusOrAccessModelTestFixture.h"
10 #include "MockHeaders.h"
11 #include "llvm/Support/ErrorHandling.h"
12
13 #include <string>
14 #include <utility>
15 #include <vector>
16
17 #include "gtest/gtest.h"
18
19 namespace clang::dataflow::statusor_model {
20 namespace {
21
22 TEST_P(UncheckedStatusOrAccessModelTest, NoStatusOrMention) {
23 ExpectDiagnosticsFor(R"cc(
24 void target() { "nop"; }
25 )cc");
26 }
27
28 TEST_P(UncheckedStatusOrAccessModelTest,
29 UnwrapWithoutCheck_Lvalue_CallToValue) {
30 ExpectDiagnosticsFor(R"cc(
31 #include "unchecked_statusor_access_test_defs.h"
32
33 void target(STATUSOR_INT sor) {
34 sor.value(); // [[unsafe]]
35 }
36 )cc");
37 }
38
39 TEST_P(UncheckedStatusOrAccessModelTest, NonExplicitInitialization) {
40 ExpectDiagnosticsFor(R"cc(
41 #include "unchecked_statusor_access_test_defs.h"
42 STATUSOR_INT target() {
43 STATUSOR_INT x = Make<STATUSOR_INT>();
44 return x.value(); // [[unsafe]]
45 }
46 )cc");
47 }
48
49 TEST_P(UncheckedStatusOrAccessModelTest,
50 UnwrapWithoutCheck_Lvalue_CallToValue_NewLine) {
51 ExpectDiagnosticsFor(R"cc(
52 #include "unchecked_statusor_access_test_defs.h"
53
54 void target(STATUSOR_INT sor) {
55 sor. // force newline
56 value(); // [[unsafe]]
57 }
58 )cc");
59 }
60
61 TEST_P(UncheckedStatusOrAccessModelTest,
62 UnwrapWithoutCheck_Rvalue_CallToValue) {
63 ExpectDiagnosticsFor(R"cc(
64 #include "unchecked_statusor_access_test_defs.h"
65
66 void target(STATUSOR_INT sor) {
67 std::move(sor).value(); // [[unsafe]]
68 }
69 )cc");
70 }
71
72 TEST_P(UncheckedStatusOrAccessModelTest,
73 UnwrapWithoutCheck_Lvalue_CallToValueOrDie) {
74 ExpectDiagnosticsFor(R"cc(
75 #include "unchecked_statusor_access_test_defs.h"
76
77 void target(STATUSOR_INT sor) {
78 sor.ValueOrDie(); // [[unsafe]]
79 }
80 )cc");
81 }
82
83 TEST_P(UncheckedStatusOrAccessModelTest,
84 UnwrapWithoutCheck_Rvalue_CallToValueOrDie) {
85 ExpectDiagnosticsFor(R"cc(
86 #include "unchecked_statusor_access_test_defs.h"
87
88 void target(STATUSOR_INT sor) {
89 std::move(sor).ValueOrDie(); // [[unsafe]]
90 }
91 )cc");
92 }
93
94 TEST_P(UncheckedStatusOrAccessModelTest,
95 UnwrapWithoutCheck_Lvalue_CallToOperatorStar) {
96 ExpectDiagnosticsFor(R"cc(
97 #include "unchecked_statusor_access_test_defs.h"
98
99 void target(STATUSOR_INT sor) {
100 *sor; // [[unsafe]]
101 }
102 )cc");
103 }
104
105 TEST_P(UncheckedStatusOrAccessModelTest,
106 UnwrapWithoutCheck_Lvalue_CallToOperatorStarSeparateLine) {
107 ExpectDiagnosticsFor(R"cc(
108 #include "unchecked_statusor_access_test_defs.h"
109
110 void target(STATUSOR_INT sor) {
111 * // [[unsafe]]
112 sor;
113 }
114 )cc");
115 }
116
117 TEST_P(UncheckedStatusOrAccessModelTest,
118 UnwrapWithoutCheck_Rvalue_CallToOperatorStar) {
119 ExpectDiagnosticsFor(R"cc(
120 #include "unchecked_statusor_access_test_defs.h"
121
122 void target(STATUSOR_INT sor) {
123 *std::move(sor); // [[unsafe]]
124 }
125 )cc");
126 }
127
128 TEST_P(UncheckedStatusOrAccessModelTest,
129 UnwrapWithoutCheck_Lvalue_CallToOperatorArrow) {
130 ExpectDiagnosticsFor(R"cc(
131 #include "unchecked_statusor_access_test_defs.h"
132
133 struct Foo {
134 void foo();
135 };
136
137 void target(absl::StatusOr<Foo> sor) {
138 sor->foo(); // [[unsafe]]
139 }
140 )cc");
141 }
142
143 TEST_P(UncheckedStatusOrAccessModelTest,
144 UnwrapWithoutCheck_Rvalue_CallToOperatorArrow) {
145 ExpectDiagnosticsFor(R"cc(
146 #include "unchecked_statusor_access_test_defs.h"
147
148 struct Foo {
149 void foo();
150 };
151
152 void target(absl::StatusOr<Foo> sor) {
153 std::move(sor)->foo(); // [[unsafe]]
154 }
155 )cc");
156 }
157
158 TEST_P(UncheckedStatusOrAccessModelTest, UnwrapRvalueWithCheck) {
159 ExpectDiagnosticsFor(R"cc(
160 #include "unchecked_statusor_access_test_defs.h"
161
162 void target(STATUSOR_INT sor) {
163 if (sor.ok()) std::move(sor).value();
164 }
165 )cc");
166 }
167
168 TEST_P(UncheckedStatusOrAccessModelTest, ParensInDeclInitExpr) {
169 ExpectDiagnosticsFor(R"cc(
170 #include "unchecked_statusor_access_test_defs.h"
171
172 void target() {
173 auto sor = (Make<STATUSOR_INT>());
174 if (sor.ok()) sor.value();
175 }
176 )cc");
177 }
178
179 TEST_P(UncheckedStatusOrAccessModelTest, ReferenceInDeclInitExpr) {
180 ExpectDiagnosticsFor(R"cc(
181 #include "unchecked_statusor_access_test_defs.h"
182
183 struct Foo {
184 const STATUSOR_INT& GetStatusOrInt() const;
185 };
186
187 void target(Foo foo) {
188 auto sor = foo.GetStatusOrInt();
189 if (sor.ok()) sor.value();
190 }
191 )cc");
192 ExpectDiagnosticsFor(R"cc(
193 #include "unchecked_statusor_access_test_defs.h"
194
195 struct Foo {
196 STATUSOR_INT& GetStatusOrInt();
197 };
198
199 void target(Foo foo) {
200 auto sor = foo.GetStatusOrInt();
201 if (sor.ok()) sor.value();
202 }
203 )cc");
204 ExpectDiagnosticsFor(R"cc(
205 #include "unchecked_statusor_access_test_defs.h"
206
207 struct Foo {
208 STATUSOR_INT&& GetStatusOrInt() &&;
209 };
210
211 void target(Foo foo) {
212 auto sor = std::move(foo).GetStatusOrInt();
213 if (sor.ok()) sor.value();
214 }
215 )cc");
216 }
217
218 TEST_P(UncheckedStatusOrAccessModelTest, IfThenElse) {
219 ExpectDiagnosticsFor(
220 R"cc(
221 #include "unchecked_statusor_access_test_defs.h"
222
223 void target(STATUSOR_INT sor) {
224 if (sor.ok())
225 sor.value();
226 else
227 sor.value(); // [[unsafe]]
228
229 sor.value(); // [[unsafe]]
230 }
231 )cc");
232 ExpectDiagnosticsFor(R"cc(
233 #include "unchecked_statusor_access_test_defs.h"
234
235 void target() {
236 if (auto sor = Make<STATUSOR_INT>(); sor.ok())
237 sor.value();
238 else
239 sor.value(); // [[unsafe]]
240 }
241 )cc");
242 }
243
244 TEST_P(UncheckedStatusOrAccessModelTest, JoinSafeSafe) {
245 ExpectDiagnosticsFor(
246 R"cc(
247 #include "unchecked_statusor_access_test_defs.h"
248
249 void target(STATUSOR_INT sor, bool b) {
250 if (sor.ok()) {
251 if (b)
252 sor.value();
253 else
254 sor.value();
255 }
256 }
257 )cc");
258 }
259
260 TEST_P(UncheckedStatusOrAccessModelTest, JoinUnsafeUnsafe) {
261 ExpectDiagnosticsFor(R"cc(
262 #include "unchecked_statusor_access_test_defs.h"
263
264 void target(STATUSOR_INT sor, bool b) {
265 if (b)
266 sor.value(); // [[unsafe]]
267 else
268 sor.value(); // [[unsafe]]
269 }
270 )cc");
271 }
272
273 TEST_P(UncheckedStatusOrAccessModelTest, InversedIfThenElse) {
274 ExpectDiagnosticsFor(
275 R"cc(
276 #include "unchecked_statusor_access_test_defs.h"
277
278 void target(STATUSOR_INT sor) {
279 if (!sor.ok())
280 sor.value(); // [[unsafe]]
281 else
282 sor.value();
283
284 sor.value(); // [[unsafe]]
285 }
286 )cc");
287 }
288
289 TEST_P(UncheckedStatusOrAccessModelTest, DoubleInversedIfThenElse) {
290 ExpectDiagnosticsFor(R"cc(
291 #include "unchecked_statusor_access_test_defs.h"
292
293 void target(STATUSOR_INT sor) {
294 if (!!sor.ok())
295 sor.value();
296 else
297 sor.value(); // [[unsafe]]
298 }
299 )cc");
300 }
301
302 TEST_P(UncheckedStatusOrAccessModelTest, TripleInversedIfThenElse) {
303 ExpectDiagnosticsFor(R"cc(
304 #include "unchecked_statusor_access_test_defs.h"
305
306 void target(STATUSOR_INT sor) {
307 if (!!!sor.ok())
308 sor.value(); // [[unsafe]]
309 else
310 sor.value();
311 }
312 )cc");
313 }
314
315 TEST_P(UncheckedStatusOrAccessModelTest, IfThenElse_LhsAndRhs) {
316 ExpectDiagnosticsFor(R"cc(
317 #include "unchecked_statusor_access_test_defs.h"
318
319 void target(STATUSOR_INT x, STATUSOR_INT y) {
320 if (x.ok() && y.ok()) {
321 x.value();
322
323 y.value();
324 } else {
325 x.value(); // [[unsafe]]
326
327 y.value(); // [[unsafe]]
328 }
329 }
330 )cc");
331 }
332
333 TEST_P(UncheckedStatusOrAccessModelTest, IfThenElse_NotLhsAndRhs) {
334 ExpectDiagnosticsFor(
335 R"cc(
336 #include "unchecked_statusor_access_test_defs.h"
337
338 void target(STATUSOR_INT x, STATUSOR_INT y) {
339 if (!x.ok() && y.ok()) {
340 y.value();
341
342 x.value(); // [[unsafe]]
343 } else {
344 x.value(); // [[unsafe]]
345
346 y.value(); // [[unsafe]]
347 }
348 }
349 )cc");
350 }
351
352 TEST_P(UncheckedStatusOrAccessModelTest, IfThenElse_LhsAndNotRhs) {
353 ExpectDiagnosticsFor(
354 R"cc(
355 #include "unchecked_statusor_access_test_defs.h"
356
357 void target(STATUSOR_INT x, STATUSOR_INT y) {
358 if (x.ok() && !y.ok()) {
359 x.value();
360
361 y.value(); // [[unsafe]]
362 } else {
363 x.value(); // [[unsafe]]
364
365 y.value(); // [[unsafe]]
366 }
367 }
368 )cc");
369 }
370
371 TEST_P(UncheckedStatusOrAccessModelTest, IfThenElse_NotLhsAndNotRhs) {
372 ExpectDiagnosticsFor(R"cc(
373 #include "unchecked_statusor_access_test_defs.h"
374
375 void target(STATUSOR_INT x, STATUSOR_INT y) {
376 if (!x.ok() && !y.ok()) {
377 x.value(); // [[unsafe]]
378
379 y.value(); // [[unsafe]]
380 } else {
381 x.value(); // [[unsafe]]
382
383 y.value(); // [[unsafe]]
384 }
385 }
386 )cc");
387 }
388
389 TEST_P(UncheckedStatusOrAccessModelTest, IfThenElse_Not_LhsAndRhs) {
390 ExpectDiagnosticsFor(
391 R"cc(
392 #include "unchecked_statusor_access_test_defs.h"
393
394 void target(STATUSOR_INT x, STATUSOR_INT y) {
395 if (!(x.ok() && y.ok())) {
396 x.value(); // [[unsafe]]
397
398 y.value(); // [[unsafe]]
399 } else {
400 x.value();
401
402 y.value();
403 }
404 }
405 )cc");
406 }
407
408 TEST_P(UncheckedStatusOrAccessModelTest, IfThenElse_Not_NotLhsAndRhs) {
409 ExpectDiagnosticsFor(
410 R"cc(
411 #include "unchecked_statusor_access_test_defs.h"
412
413 void target(STATUSOR_INT x, STATUSOR_INT y) {
414 if (!(!x.ok() && y.ok())) {
415 x.value(); // [[unsafe]]
416
417 y.value(); // [[unsafe]]
418 } else {
419 y.value();
420
421 x.value(); // [[unsafe]]
422 }
423 }
424 )cc");
425 }
426
427 TEST_P(UncheckedStatusOrAccessModelTest, IfThenElse_Not_LhsAndNotRhs) {
428 ExpectDiagnosticsFor(
429 R"cc(
430 #include "unchecked_statusor_access_test_defs.h"
431
432 void target(STATUSOR_INT x, STATUSOR_INT y) {
433 if (!(x.ok() && !y.ok())) {
434 x.value(); // [[unsafe]]
435
436 y.value(); // [[unsafe]]
437 } else {
438 x.value();
439
440 y.value(); // [[unsafe]]
441 }
442 }
443 )cc");
444 }
445
446 TEST_P(UncheckedStatusOrAccessModelTest, IfThenElse_Not_NotLhsAndNotRhs) {
447 ExpectDiagnosticsFor(R"cc(
448 #include "unchecked_statusor_access_test_defs.h"
449
450 void target(STATUSOR_INT x, STATUSOR_INT y) {
451 if (!(!x.ok() && !y.ok())) {
452 x.value(); // [[unsafe]]
453
454 y.value(); // [[unsafe]]
455 } else {
456 x.value(); // [[unsafe]]
457
458 y.value(); // [[unsafe]]
459 }
460 }
461 )cc");
462 }
463
464 TEST_P(UncheckedStatusOrAccessModelTest, IfThenElse_LhsOrRhs) {
465 ExpectDiagnosticsFor(R"cc(
466 #include "unchecked_statusor_access_test_defs.h"
467
468 void target(STATUSOR_INT x, STATUSOR_INT y) {
469 if (x.ok() || y.ok()) {
470 x.value(); // [[unsafe]]
471
472 y.value(); // [[unsafe]]
473 } else {
474 x.value(); // [[unsafe]]
475
476 y.value(); // [[unsafe]]
477 }
478 }
479 )cc");
480 }
481
482 TEST_P(UncheckedStatusOrAccessModelTest, IfThenElse_NotLhsOrRhs) {
483 ExpectDiagnosticsFor(
484 R"cc(
485 #include "unchecked_statusor_access_test_defs.h"
486
487 void target(STATUSOR_INT x, STATUSOR_INT y) {
488 if (!x.ok() || y.ok()) {
489 x.value(); // [[unsafe]]
490
491 y.value(); // [[unsafe]]
492 } else {
493 x.value();
494
495 y.value(); // [[unsafe]]
496 }
497 }
498 )cc");
499 }
500
501 TEST_P(UncheckedStatusOrAccessModelTest, IfThenElse_LhsOrNotRhs) {
502 ExpectDiagnosticsFor(
503 R"cc(
504 #include "unchecked_statusor_access_test_defs.h"
505
506 void target(STATUSOR_INT x, STATUSOR_INT y) {
507 if (x.ok() || !y.ok()) {
508 x.value(); // [[unsafe]]
509
510 y.value(); // [[unsafe]]
511 } else {
512 y.value();
513
514 x.value(); // [[unsafe]]
515 }
516 }
517 )cc");
518 }
519
520 TEST_P(UncheckedStatusOrAccessModelTest, IfThenElse_NotLhsOrNotRhs) {
521 ExpectDiagnosticsFor(
522 R"cc(
523 #include "unchecked_statusor_access_test_defs.h"
524
525 void target(STATUSOR_INT x, STATUSOR_INT y) {
526 if (!x.ok() || !y.ok()) {
527 x.value(); // [[unsafe]]
528
529 y.value(); // [[unsafe]]
530 } else {
531 x.value();
532
533 y.value();
534 }
535 }
536 )cc");
537 }
538
539 TEST_P(UncheckedStatusOrAccessModelTest, IfThenElse_Not_LhsOrRhs) {
540 ExpectDiagnosticsFor(R"cc(
541 #include "unchecked_statusor_access_test_defs.h"
542
543 void target(STATUSOR_INT x, STATUSOR_INT y) {
544 if (!(x.ok() || y.ok())) {
545 x.value(); // [[unsafe]]
546
547 y.value(); // [[unsafe]]
548 } else {
549 x.value(); // [[unsafe]]
550
551 y.value(); // [[unsafe]]
552 }
553 }
554 )cc");
555 }
556
557 TEST_P(UncheckedStatusOrAccessModelTest, IfThenElse_Not_NotLhsOrRhs) {
558 ExpectDiagnosticsFor(
559 R"cc(
560 #include "unchecked_statusor_access_test_defs.h"
561
562 void target(STATUSOR_INT x, STATUSOR_INT y) {
563 if (!(!x.ok() || y.ok())) {
564 x.value();
565
566 y.value(); // [[unsafe]]
567 } else {
568 x.value(); // [[unsafe]]
569
570 y.value(); // [[unsafe]]
571 }
572 }
573 )cc");
574 }
575
576 TEST_P(UncheckedStatusOrAccessModelTest, IfThenElse_Not_LhsOrNotRhs) {
577 ExpectDiagnosticsFor(
578 R"cc(
579 #include "unchecked_statusor_access_test_defs.h"
580
581 void target(STATUSOR_INT x, STATUSOR_INT y) {
582 if (!(x.ok() || !y.ok())) {
583 y.value();
584
585 x.value(); // [[unsafe]]
586 } else {
587 x.value(); // [[unsafe]]
588
589 y.value(); // [[unsafe]]
590 }
591 }
592 )cc");
593 }
594
595 TEST_P(UncheckedStatusOrAccessModelTest, IfThenElse_Not_NotLhsOrNotRhs) {
596 ExpectDiagnosticsFor(R"cc(
597 #include "unchecked_statusor_access_test_defs.h"
598
599 void target(STATUSOR_INT x, STATUSOR_INT y) {
600 if (!(!x.ok() || !y.ok())) {
601 x.value();
602
603 y.value();
604 } else {
605 x.value(); // [[unsafe]]
606
607 y.value(); // [[unsafe]]
608 }
609 }
610 )cc");
611 }
612
613 TEST_P(UncheckedStatusOrAccessModelTest, TerminatingIfThenBranch) {
614 ExpectDiagnosticsFor(R"cc(
615 #include "unchecked_statusor_access_test_defs.h"
616
617 void target(STATUSOR_INT sor) {
618 if (!sor.ok()) return;
619
620 sor.value();
621 }
622 )cc");
623 ExpectDiagnosticsFor(R"cc(
624 #include "unchecked_statusor_access_test_defs.h"
625
626 void target(STATUSOR_INT sor) {
627 if (sor.ok()) return;
628
629 sor.value(); // [[unsafe]]
630 }
631 )cc");
632 ExpectDiagnosticsFor(
633 R"cc(
634 #include "unchecked_statusor_access_test_defs.h"
635
636 void target(STATUSOR_INT x, STATUSOR_INT y) {
637 if (!x.ok() || !y.ok()) return;
638
639 x.value();
640
641 y.value();
642 }
643 )cc");
644 }
645
646 TEST_P(UncheckedStatusOrAccessModelTest, TerminatingIfElseBranch) {
647 ExpectDiagnosticsFor(R"cc(
648 #include "unchecked_statusor_access_test_defs.h"
649
650 void target(STATUSOR_INT sor) {
651 if (sor.ok()) {
652 } else {
653 return;
654 }
655
656 sor.value();
657 }
658 )cc");
659 ExpectDiagnosticsFor(R"cc(
660 #include "unchecked_statusor_access_test_defs.h"
661
662 void target(STATUSOR_INT sor) {
663 if (!sor.ok()) {
664 } else {
665 return;
666 }
667
668 sor.value(); // [[unsafe]]
669 }
670 )cc");
671 }
672
673 TEST_P(UncheckedStatusOrAccessModelTest, TerminatingIfThenBranchInLoop) {
674 ExpectDiagnosticsFor(R"cc(
675 #include "unchecked_statusor_access_test_defs.h"
676
677 void target(STATUSOR_INT sor) {
678 while (Make<bool>()) {
679 if (!sor.ok()) continue;
680
681 sor.value();
682 }
683
684 sor.value(); // [[unsafe]]
685 }
686 )cc");
687 ExpectDiagnosticsFor(R"cc(
688 #include "unchecked_statusor_access_test_defs.h"
689
690 void target(STATUSOR_INT sor) {
691 while (Make<bool>()) {
692 if (!sor.ok()) break;
693
694 sor.value();
695 }
696
697 sor.value(); // [[unsafe]]
698 }
699 )cc");
700 }
701
702 TEST_P(UncheckedStatusOrAccessModelTest, TernaryConditionalOperator) {
703 ExpectDiagnosticsFor(R"cc(
704 #include "unchecked_statusor_access_test_defs.h"
705
706 void target(STATUSOR_INT sor) {
707 sor.ok() ? sor.value() : 21;
708
709 sor.ok() ? 21 : sor.value(); // [[unsafe]]
710 }
711 )cc");
712 ExpectDiagnosticsFor(R"cc(
713 #include "unchecked_statusor_access_test_defs.h"
714
715 void target(STATUSOR_INT sor) {
716 !sor.ok() ? 21 : sor.value();
717
718 !sor.ok() ? sor.value() : 21; // [[unsafe]]
719 }
720 )cc");
721 ExpectDiagnosticsFor(R"cc(
722 #include "unchecked_statusor_access_test_defs.h"
723
724 void target(STATUSOR_INT sor1, STATUSOR_INT sor2) {
725 !((__builtin_expect(false || (!(sor1.ok() && sor2.ok())), false)))
726 ? (void)0
727 : (void)1;
728 do {
729 sor1.value(); // [[unsafe]]
730 sor2.value(); // [[unsafe]]
731 } while (true);
732 }
733 )cc");
734 }
735
736 TEST_P(UncheckedStatusOrAccessModelTest, While) {
737 ExpectDiagnosticsFor(R"cc(
738 #include "unchecked_statusor_access_test_defs.h"
739
740 void target(STATUSOR_INT sor) {
741 while (Make<bool>()) sor.value(); // [[unsafe]]
742 }
743 )cc");
744 ExpectDiagnosticsFor(R"cc(
745 #include "unchecked_statusor_access_test_defs.h"
746
747 void target(STATUSOR_INT sor) {
748 while (sor.ok()) sor.value();
749 }
750 )cc");
751 ExpectDiagnosticsFor(R"cc(
752 #include "unchecked_statusor_access_test_defs.h"
753
754 void target(STATUSOR_INT sor) {
755 while (!sor.ok()) sor.value(); // [[unsafe]]
756 }
757 )cc");
758 ExpectDiagnosticsFor(R"cc(
759 #include "unchecked_statusor_access_test_defs.h"
760
761 void target(STATUSOR_INT sor) {
762 while (!!sor.ok()) sor.value();
763 }
764 )cc");
765 ExpectDiagnosticsFor(R"cc(
766 #include "unchecked_statusor_access_test_defs.h"
767
768 void target(STATUSOR_INT sor) {
769 while (!!!sor.ok()) sor.value(); // [[unsafe]]
770 }
771 )cc");
772 }
773
774 TEST_P(UncheckedStatusOrAccessModelTest, While_LhsAndRhs) {
775 ExpectDiagnosticsFor(
776 R"cc(
777 #include "unchecked_statusor_access_test_defs.h"
778
779 void target(STATUSOR_INT x, STATUSOR_INT y) {
780 while (x.ok() && y.ok()) {
781 x.value();
782
783 y.value();
784 }
785 }
786 )cc");
787 }
788
789 TEST_P(UncheckedStatusOrAccessModelTest, While_NotLhsAndRhs) {
790 ExpectDiagnosticsFor(R"cc(
791 #include "unchecked_statusor_access_test_defs.h"
792
793 void target(STATUSOR_INT x, STATUSOR_INT y) {
794 while (!x.ok() && y.ok()) x.value(); // [[unsafe]]
795 }
796 )cc");
797 ExpectDiagnosticsFor(R"cc(
798 #include "unchecked_statusor_access_test_defs.h"
799
800 void target(STATUSOR_INT x, STATUSOR_INT y) {
801 while (!x.ok() && y.ok()) y.value();
802 }
803 )cc");
804 }
805
806 TEST_P(UncheckedStatusOrAccessModelTest, While_LhsAndNotRhs) {
807 ExpectDiagnosticsFor(R"cc(
808 #include "unchecked_statusor_access_test_defs.h"
809
810 void target(STATUSOR_INT x, STATUSOR_INT y) {
811 while (x.ok() && !y.ok()) x.value();
812 }
813 )cc");
814 ExpectDiagnosticsFor(R"cc(
815 #include "unchecked_statusor_access_test_defs.h"
816
817 void target(STATUSOR_INT x, STATUSOR_INT y) {
818 while (x.ok() && !y.ok()) y.value(); // [[unsafe]]
819 }
820 )cc");
821 }
822
823 TEST_P(UncheckedStatusOrAccessModelTest, While_NotLhsAndNotRhs) {
824 ExpectDiagnosticsFor(R"cc(
825 #include "unchecked_statusor_access_test_defs.h"
826
827 void target(STATUSOR_INT x, STATUSOR_INT y) {
828 while (!x.ok() && !y.ok()) x.value(); // [[unsafe]]
829 }
830 )cc");
831 ExpectDiagnosticsFor(R"cc(
832 #include "unchecked_statusor_access_test_defs.h"
833
834 void target(STATUSOR_INT x, STATUSOR_INT y) {
835 while (!x.ok() && !y.ok()) y.value(); // [[unsafe]]
836 }
837 )cc");
838 }
839
840 TEST_P(UncheckedStatusOrAccessModelTest, While_Not_LhsAndRhs) {
841 ExpectDiagnosticsFor(R"cc(
842 #include "unchecked_statusor_access_test_defs.h"
843
844 void target(STATUSOR_INT x, STATUSOR_INT y) {
845 while (!(x.ok() && y.ok())) x.value(); // [[unsafe]]
846 }
847 )cc");
848 ExpectDiagnosticsFor(R"cc(
849 #include "unchecked_statusor_access_test_defs.h"
850
851 void target(STATUSOR_INT x, STATUSOR_INT y) {
852 while (!(x.ok() && y.ok())) y.value(); // [[unsafe]]
853 }
854 )cc");
855 }
856
857 TEST_P(UncheckedStatusOrAccessModelTest, While_Not_NotLhsAndRhs) {
858 ExpectDiagnosticsFor(R"cc(
859 #include "unchecked_statusor_access_test_defs.h"
860
861 void target(STATUSOR_INT x, STATUSOR_INT y) {
862 while (!(!x.ok() && y.ok())) x.value(); // [[unsafe]]
863 }
864 )cc");
865 ExpectDiagnosticsFor(R"cc(
866 #include "unchecked_statusor_access_test_defs.h"
867
868 void target(STATUSOR_INT x, STATUSOR_INT y) {
869 while (!(!x.ok() && y.ok())) y.value(); // [[unsafe]]
870 }
871 )cc");
872 }
873
874 TEST_P(UncheckedStatusOrAccessModelTest, While_Not_LhsAndNotRhs) {
875 ExpectDiagnosticsFor(R"cc(
876 #include "unchecked_statusor_access_test_defs.h"
877
878 void target(STATUSOR_INT x, STATUSOR_INT y) {
879 while (!(x.ok() && !y.ok())) x.value(); // [[unsafe]]
880 }
881 )cc");
882 ExpectDiagnosticsFor(R"cc(
883 #include "unchecked_statusor_access_test_defs.h"
884
885 void target(STATUSOR_INT x, STATUSOR_INT y) {
886 while (!(x.ok() && !y.ok())) y.value(); // [[unsafe]]
887 }
888 )cc");
889 }
890
891 TEST_P(UncheckedStatusOrAccessModelTest, While_Not_NotLhsAndNotRhs) {
892 ExpectDiagnosticsFor(R"cc(
893 #include "unchecked_statusor_access_test_defs.h"
894
895 void target(STATUSOR_INT x, STATUSOR_INT y) {
896 while (!(!x.ok() && !y.ok())) x.value(); // [[unsafe]]
897 }
898 )cc");
899 ExpectDiagnosticsFor(R"cc(
900 #include "unchecked_statusor_access_test_defs.h"
901
902 void target(STATUSOR_INT x, STATUSOR_INT y) {
903 while (!(!x.ok() && !y.ok())) y.value(); // [[unsafe]]
904 }
905 )cc");
906 }
907
908 TEST_P(UncheckedStatusOrAccessModelTest, While_LhsOrRhs) {
909 ExpectDiagnosticsFor(R"cc(
910 #include "unchecked_statusor_access_test_defs.h"
911
912 void target(STATUSOR_INT x, STATUSOR_INT y) {
913 while (x.ok() || y.ok()) x.value(); // [[unsafe]]
914 }
915 )cc");
916 ExpectDiagnosticsFor(R"cc(
917 #include "unchecked_statusor_access_test_defs.h"
918
919 void target(STATUSOR_INT x, STATUSOR_INT y) {
920 while (x.ok() || y.ok()) y.value(); // [[unsafe]]
921 }
922 )cc");
923 }
924
925 TEST_P(UncheckedStatusOrAccessModelTest, While_NotLhsOrRhs) {
926 ExpectDiagnosticsFor(R"cc(
927 #include "unchecked_statusor_access_test_defs.h"
928
929 void target(STATUSOR_INT x, STATUSOR_INT y) {
930 while (!x.ok() || y.ok()) x.value(); // [[unsafe]]
931 }
932 )cc");
933 ExpectDiagnosticsFor(R"cc(
934 #include "unchecked_statusor_access_test_defs.h"
935
936 void target(STATUSOR_INT x, STATUSOR_INT y) {
937 while (!x.ok() || y.ok()) y.value(); // [[unsafe]]
938 }
939 )cc");
940 }
941
942 TEST_P(UncheckedStatusOrAccessModelTest, While_LhsOrNotRhs) {
943 ExpectDiagnosticsFor(R"cc(
944 #include "unchecked_statusor_access_test_defs.h"
945
946 void target(STATUSOR_INT x, STATUSOR_INT y) {
947 while (x.ok() || !y.ok()) x.value(); // [[unsafe]]
948 }
949 )cc");
950 ExpectDiagnosticsFor(R"cc(
951 #include "unchecked_statusor_access_test_defs.h"
952
953 void target(STATUSOR_INT x, STATUSOR_INT y) {
954 while (x.ok() || !y.ok()) y.value(); // [[unsafe]]
955 }
956 )cc");
957 }
958
959 TEST_P(UncheckedStatusOrAccessModelTest, While_NotLhsOrNotRhs) {
960 ExpectDiagnosticsFor(R"cc(
961 #include "unchecked_statusor_access_test_defs.h"
962
963 void target(STATUSOR_INT x, STATUSOR_INT y) {
964 while (!x.ok() || !y.ok()) x.value(); // [[unsafe]]
965 }
966 )cc");
967 ExpectDiagnosticsFor(R"cc(
968 #include "unchecked_statusor_access_test_defs.h"
969
970 void target(STATUSOR_INT x, STATUSOR_INT y) {
971 while (!x.ok() || !y.ok()) y.value(); // [[unsafe]]
972 }
973 )cc");
974 }
975
976 TEST_P(UncheckedStatusOrAccessModelTest, While_Not_LhsOrRhs) {
977 ExpectDiagnosticsFor(R"cc(
978 #include "unchecked_statusor_access_test_defs.h"
979
980 void target(STATUSOR_INT x, STATUSOR_INT y) {
981 while (!(x.ok() || y.ok())) x.value(); // [[unsafe]]
982 }
983 )cc");
984 ExpectDiagnosticsFor(R"cc(
985 #include "unchecked_statusor_access_test_defs.h"
986
987 void target(STATUSOR_INT x, STATUSOR_INT y) {
988 while (!(x.ok() || y.ok())) y.value(); // [[unsafe]]
989 }
990 )cc");
991 }
992
993 TEST_P(UncheckedStatusOrAccessModelTest, While_Not_NotLhsOrRhs) {
994 ExpectDiagnosticsFor(R"cc(
995 #include "unchecked_statusor_access_test_defs.h"
996
997 void target(STATUSOR_INT x, STATUSOR_INT y) {
998 while (!(!x.ok() || y.ok())) x.value();
999 }
1000 )cc");
1001 ExpectDiagnosticsFor(R"cc(
1002 #include "unchecked_statusor_access_test_defs.h"
1003
1004 void target(STATUSOR_INT x, STATUSOR_INT y) {
1005 while (!(!x.ok() || y.ok())) y.value(); // [[unsafe]]
1006 }
1007 )cc");
1008 }
1009
1010 TEST_P(UncheckedStatusOrAccessModelTest, While_Not_LhsOrNotRhs) {
1011 ExpectDiagnosticsFor(R"cc(
1012 #include "unchecked_statusor_access_test_defs.h"
1013
1014 void target(STATUSOR_INT x, STATUSOR_INT y) {
1015 while (!(x.ok() || !y.ok())) x.value(); // [[unsafe]]
1016 }
1017 )cc");
1018 ExpectDiagnosticsFor(R"cc(
1019 #include "unchecked_statusor_access_test_defs.h"
1020
1021 void target(STATUSOR_INT x, STATUSOR_INT y) {
1022 while (!(x.ok() || !y.ok())) y.value();
1023 }
1024 )cc");
1025 }
1026
1027 TEST_P(UncheckedStatusOrAccessModelTest, While_Not_NotLhsOrNotRhs) {
1028 ExpectDiagnosticsFor(
1029 R"cc(
1030 #include "unchecked_statusor_access_test_defs.h"
1031
1032 void target(STATUSOR_INT x, STATUSOR_INT y) {
1033 while (!(!x.ok() || !y.ok())) {
1034 x.value();
1035
1036 y.value();
1037 }
1038 }
1039 )cc");
1040 }
1041
1042 TEST_P(UncheckedStatusOrAccessModelTest, While_AccessAfterStmt) {
1043 ExpectDiagnosticsFor(R"cc(
1044 #include "unchecked_statusor_access_test_defs.h"
1045
1046 void target(STATUSOR_INT sor) {
1047 while (sor.ok()) {
1048 }
1049
1050 sor.value(); // [[unsafe]]
1051 }
1052 )cc");
1053 ExpectDiagnosticsFor(R"cc(
1054 #include "unchecked_statusor_access_test_defs.h"
1055
1056 void target(STATUSOR_INT sor) {
1057 while (!sor.ok()) {
1058 }
1059
1060 sor.value();
1061 }
1062 )cc");
1063 }
1064
1065 TEST_P(UncheckedStatusOrAccessModelTest, While_TerminatingBranch_Return) {
1066 ExpectDiagnosticsFor(R"cc(
1067 #include "unchecked_statusor_access_test_defs.h"
1068
1069 void target(STATUSOR_INT sor) {
1070 while (!sor.ok()) return;
1071
1072 sor.value();
1073 }
1074 )cc");
1075 ExpectDiagnosticsFor(R"cc(
1076 #include "unchecked_statusor_access_test_defs.h"
1077
1078 void target(STATUSOR_INT sor) {
1079 while (sor.ok()) return;
1080
1081 sor.value(); // [[unsafe]]
1082 }
1083 )cc");
1084 }
1085
1086 TEST_P(UncheckedStatusOrAccessModelTest, While_NestedIfWithBinaryCondition) {
1087 ExpectDiagnosticsFor(
1088 R"cc(
1089 #include "unchecked_statusor_access_test_defs.h"
1090
1091 void target(STATUSOR_INT x, STATUSOR_INT y) {
1092 while (Make<bool>()) {
1093 if (x.ok() && y.ok()) {
1094 x.value();
1095
1096 y.value();
1097 }
1098 }
1099 }
1100 )cc");
1101 ExpectDiagnosticsFor(
1102 R"cc(
1103 #include "unchecked_statusor_access_test_defs.h"
1104
1105 void target(STATUSOR_INT x, STATUSOR_INT y) {
1106 while (Make<bool>()) {
1107 if (!(!x.ok() || !y.ok())) {
1108 x.value();
1109
1110 y.value();
1111 }
1112 }
1113 }
1114 )cc");
1115 }
1116
1117 TEST_P(UncheckedStatusOrAccessModelTest, BuiltinExpect) {
1118 ExpectDiagnosticsFor(
1119 R"cc(
1120 #include "unchecked_statusor_access_test_defs.h"
1121
1122 void target(STATUSOR_INT x, STATUSOR_INT y) {
1123 if (!__builtin_expect(!x.ok() || __builtin_expect(!y.ok(), true), false)) {
1124 x.value();
1125
1126 y.value();
1127 }
1128 }
1129 )cc");
1130 }
1131
1132 TEST_P(UncheckedStatusOrAccessModelTest, CopyAssignment) {
1133 ExpectDiagnosticsFor(R"cc(
1134 #include "unchecked_statusor_access_test_defs.h"
1135
1136 void target() {
1137 STATUSOR_INT sor = Make<STATUSOR_INT>();
1138 if (sor.ok()) {
1139 sor = Make<STATUSOR_INT>();
1140 sor.value(); // [[unsafe]]
1141 }
1142 }
1143 )cc");
1144 ExpectDiagnosticsFor(R"cc(
1145 #include "unchecked_statusor_access_test_defs.h"
1146
1147 void target() {
1148 STATUSOR_INT sor = Make<STATUSOR_INT>();
1149 if (!sor.ok()) return;
1150
1151 sor = Make<STATUSOR_INT>();
1152 sor.value(); // [[unsafe]]
1153 }
1154 )cc");
1155 ExpectDiagnosticsFor(R"cc(
1156 #include "unchecked_statusor_access_test_defs.h"
1157
1158 void target() {
1159 STATUSOR_INT x = Make<STATUSOR_INT>();
1160 if (x.ok()) {
1161 STATUSOR_INT y = x;
1162 x = Make<STATUSOR_INT>();
1163
1164 y.value();
1165
1166 x.value(); // [[unsafe]]
1167 }
1168 }
1169 )cc");
1170 ExpectDiagnosticsFor(
1171 R"cc(
1172 #include "unchecked_statusor_access_test_defs.h"
1173
1174 void target() {
1175 STATUSOR_INT x = Make<STATUSOR_INT>();
1176 STATUSOR_INT y = x;
1177 if (!y.ok()) return;
1178
1179 x.value();
1180
1181 y = Make<STATUSOR_INT>();
1182 x.value();
1183 }
1184 )cc");
1185 ExpectDiagnosticsFor(R"cc(
1186 #include "unchecked_statusor_access_test_defs.h"
1187
1188 struct Foo {
1189 STATUSOR_INT bar;
1190 };
1191
1192 void target(Foo foo) {
1193 foo.bar = Make<STATUSOR_INT>();
1194 if (foo.bar.ok()) {
1195 foo.bar.value();
1196
1197 foo.bar = Make<STATUSOR_INT>();
1198 foo.bar.value(); // [[unsafe]]
1199 }
1200 }
1201 )cc");
1202 }
1203
1204 TEST_P(UncheckedStatusOrAccessModelTest, ShortCircuitingBinaryOperators) {
1205 ExpectDiagnosticsFor(R"cc(
1206 #include "unchecked_statusor_access_test_defs.h"
1207
1208 void target(STATUSOR_BOOL sor) {
1209 bool b = sor.ok() & sor.value(); // [[unsafe]]
1210 }
1211 )cc");
1212 ExpectDiagnosticsFor(R"cc(
1213 #include "unchecked_statusor_access_test_defs.h"
1214
1215 void target(STATUSOR_BOOL sor) {
1216 bool b = sor.ok() && sor.value();
1217 }
1218 )cc");
1219 ExpectDiagnosticsFor(R"cc(
1220 #include "unchecked_statusor_access_test_defs.h"
1221
1222 void target(STATUSOR_BOOL sor) {
1223 bool b = !sor.ok() && sor.value(); // [[unsafe]]
1224 }
1225 )cc");
1226 ExpectDiagnosticsFor(R"cc(
1227 #include "unchecked_statusor_access_test_defs.h"
1228
1229 void target(STATUSOR_BOOL sor) {
1230 bool b = sor.ok() || sor.value(); // [[unsafe]]
1231 }
1232 )cc");
1233 ExpectDiagnosticsFor(R"cc(
1234 #include "unchecked_statusor_access_test_defs.h"
1235
1236 void target(STATUSOR_BOOL sor) {
1237 bool b = !sor.ok() || sor.value();
1238 }
1239 )cc");
1240 ExpectDiagnosticsFor(R"cc(
1241 #include "unchecked_statusor_access_test_defs.h"
1242
1243 void target(bool b, STATUSOR_INT sor) {
1244 if (b || sor.ok()) {
1245 do {
1246 sor.value(); // [[unsafe]]
1247 } while (true);
1248 }
1249 }
1250 )cc");
1251 ExpectDiagnosticsFor(R"cc(
1252 #include "unchecked_statusor_access_test_defs.h"
1253
1254 void target(bool b, STATUSOR_INT sor) {
1255 if (__builtin_expect(b || sor.ok(), false)) {
1256 do {
1257 sor.value(); // [[unsafe]]
1258 } while (false);
1259 }
1260 }
1261 )cc");
1262 ExpectDiagnosticsFor(
1263 R"cc(
1264 #include "unchecked_statusor_access_test_defs.h"
1265
1266 void target(STATUSOR_INT sor1, STATUSOR_INT sor2) {
1267 while (sor1.ok() && sor2.ok()) sor1.value();
1268 while (sor1.ok() && sor2.ok()) sor2.value();
1269 }
1270 )cc");
1271 }
1272
1273 TEST_P(UncheckedStatusOrAccessModelTest, References) {
1274 ExpectDiagnosticsFor(R"cc(
1275 #include "unchecked_statusor_access_test_defs.h"
1276
1277 void target() {
1278 STATUSOR_INT x = Make<STATUSOR_INT>();
1279 STATUSOR_INT& y = x;
1280 if (x.ok()) {
1281 x.value();
1282
1283 y.value();
1284 } else {
1285 x.value(); // [[unsafe]]
1286
1287 y.value(); // [[unsafe]]
1288 }
1289 }
1290 )cc");
1291 ExpectDiagnosticsFor(
1292 R"cc(
1293 #include "unchecked_statusor_access_test_defs.h"
1294
1295 void target() {
1296 STATUSOR_INT x = Make<STATUSOR_INT>();
1297 STATUSOR_INT& y = x;
1298 if (y.ok()) {
1299 x.value();
1300
1301 y.value();
1302 } else {
1303 x.value(); // [[unsafe]]
1304
1305 y.value(); // [[unsafe]]
1306 }
1307 }
1308 )cc");
1309 ExpectDiagnosticsFor(
1310 R"cc(
1311 #include "unchecked_statusor_access_test_defs.h"
1312
1313 void target() {
1314 STATUSOR_INT x = Make<STATUSOR_INT>();
1315 STATUSOR_INT& y = x;
1316 if (!y.ok()) return;
1317
1318 x.value();
1319
1320 y = Make<STATUSOR_INT>();
1321 x.value(); // [[unsafe]]
1322 }
1323 )cc");
1324 ExpectDiagnosticsFor(
1325 R"cc(
1326 #include "unchecked_statusor_access_test_defs.h"
1327
1328 void target() {
1329 STATUSOR_INT x = Make<STATUSOR_INT>();
1330 const STATUSOR_INT& y = x;
1331 if (!y.ok()) return;
1332
1333 y.value();
1334
1335 x = Make<STATUSOR_INT>();
1336 y.value(); // [[unsafe]]
1337 }
1338 )cc");
1339 }
1340
1341 TEST_P(UncheckedStatusOrAccessModelTest, NoReturnAttribute) {
1342 ExpectDiagnosticsFor(R"cc(
1343 #include "unchecked_statusor_access_test_defs.h"
1344
1345 __attribute__((noreturn)) void f();
1346
1347 void target(STATUSOR_INT sor) {
1348 if (!sor.ok()) f();
1349
1350 sor.value();
1351 }
1352 )cc");
1353 ExpectDiagnosticsFor(R"cc(
1354 #include "unchecked_statusor_access_test_defs.h"
1355
1356 void f();
1357
1358 void target(STATUSOR_INT sor) {
1359 if (!sor.ok()) f();
1360
1361 sor.value(); // [[unsafe]]
1362 }
1363 )cc");
1364 ExpectDiagnosticsFor(R"cc(
1365 #include "unchecked_statusor_access_test_defs.h"
1366
1367 struct Foo {
1368 __attribute__((noreturn)) ~Foo();
1369 void Bar();
1370 };
1371
1372 void target(STATUSOR_INT sor) {
1373 if (!sor.ok()) Foo().Bar();
1374
1375 sor.value();
1376 }
1377 )cc");
1378 ExpectDiagnosticsFor(R"cc(
1379 #include "unchecked_statusor_access_test_defs.h"
1380
1381 struct Foo {
1382 ~Foo();
1383 void Bar();
1384 };
1385
1386 void target(STATUSOR_INT sor) {
1387 if (!sor.ok()) Foo().Bar();
1388
1389 sor.value(); // [[unsafe]]
1390 }
1391 )cc");
1392 ExpectDiagnosticsFor(R"cc(
1393 #include "unchecked_statusor_access_test_defs.h"
1394
1395 void f();
1396 __attribute__((noreturn)) void g();
1397
1398 void target(STATUSOR_INT sor) {
1399 sor.ok() ? f() : g();
1400
1401 sor.value();
1402 }
1403 )cc");
1404 ExpectDiagnosticsFor(R"cc(
1405 #include "unchecked_statusor_access_test_defs.h"
1406
1407 __attribute__((noreturn)) void f();
1408 void g();
1409
1410 void target(STATUSOR_INT sor) {
1411 !sor.ok() ? f() : g();
1412
1413 sor.value();
1414 }
1415 )cc");
1416 ExpectDiagnosticsFor(R"cc(
1417 #include "unchecked_statusor_access_test_defs.h"
1418
1419 void f();
1420 void g();
1421
1422 void target(STATUSOR_INT sor) {
1423 sor.ok() ? f() : g();
1424
1425 sor.value(); // [[unsafe]]
1426 }
1427 )cc");
1428 ExpectDiagnosticsFor(R"cc(
1429 #include "unchecked_statusor_access_test_defs.h"
1430
1431 void terminate() __attribute__((noreturn));
1432
1433 void target(STATUSOR_INT sor) {
1434 sor.value(); // [[unsafe]]
1435 terminate();
1436 }
1437 )cc");
1438 ExpectDiagnosticsFor(R"cc(
1439 #include "unchecked_statusor_access_test_defs.h"
1440
1441 void terminate() __attribute__((noreturn));
1442
1443 void target(STATUSOR_INT sor) {
1444 if (sor.ok()) sor.value();
1445 terminate();
1446 }
1447 )cc");
1448 ExpectDiagnosticsFor(R"cc(
1449 #include "unchecked_statusor_access_test_defs.h"
1450
1451 void terminate() __attribute__((noreturn));
1452
1453 struct Foo {
1454 ~Foo() __attribute__((noreturn));
1455 };
1456
1457 void target() {
1458 auto sor = Make<absl::StatusOr<Foo>>();
1459 !(false || !(sor.ok())) ? (void)0 : terminate();
1460 sor.value();
1461 terminate();
1462 }
1463 )cc");
1464 }
1465
1466 TEST_P(UncheckedStatusOrAccessModelTest, DeclInLoop) {
1467 ExpectDiagnosticsFor(R"cc(
1468 #include "unchecked_statusor_access_test_defs.h"
1469
1470 void target(STATUSOR_INT sor) {
1471 while (auto ok = sor.ok()) sor.value();
1472 }
1473 )cc");
1474 ExpectDiagnosticsFor(R"cc(
1475 #include "unchecked_statusor_access_test_defs.h"
1476
1477 using BoolAlias = bool;
1478
1479 void target(STATUSOR_INT sor) {
1480 while (BoolAlias ok = sor.ok()) sor.value();
1481 }
1482 )cc");
1483 ExpectDiagnosticsFor(R"cc(
1484 #include "unchecked_statusor_access_test_defs.h"
1485
1486 void target() {
1487 while (Make<bool>()) {
1488 STATUSOR_INT sor = Make<STATUSOR_INT>();
1489 sor.value(); // [[unsafe]]
1490 }
1491 }
1492 )cc");
1493 ExpectDiagnosticsFor(R"cc(
1494 #include "unchecked_statusor_access_test_defs.h"
1495
1496 using StatusOrInt = STATUSOR_INT;
1497
1498 void target() {
1499 while (Make<bool>()) {
1500 StatusOrInt sor = Make<STATUSOR_INT>();
1501 sor.value(); // [[unsafe]]
1502 }
1503 }
1504 )cc");
1505 }
1506
1507 TEST_P(UncheckedStatusOrAccessModelTest, NonEvaluatedExprInCondition) {
1508 ExpectDiagnosticsFor(
1509 R"cc(
1510 #include "unchecked_statusor_access_test_defs.h"
1511
1512 bool unknown();
1513
1514 void target(STATUSOR_INT sor) {
1515 if (unknown() && sor.ok()) sor.value();
1516 if (sor.ok() && unknown()) sor.value();
1517 }
1518 )cc");
1519 ExpectDiagnosticsFor(
1520 R"cc(
1521 #include "unchecked_statusor_access_test_defs.h"
1522
1523 bool unknown();
1524
1525 void target(STATUSOR_INT sor) {
1526 if (!(!unknown() || !sor.ok())) sor.value();
1527 if (!(!sor.ok() || !unknown())) sor.value();
1528 }
1529 )cc");
1530 ExpectDiagnosticsFor(R"cc(
1531 #include "unchecked_statusor_access_test_defs.h"
1532
1533 bool unknown();
1534
1535 void target(STATUSOR_INT sor) {
1536 if (unknown() || sor.ok()) sor.value(); // [[unsafe]]
1537 }
1538 )cc");
1539 ExpectDiagnosticsFor(R"cc(
1540 #include "unchecked_statusor_access_test_defs.h"
1541
1542 bool unknown();
1543
1544 void target(STATUSOR_INT sor) {
1545 if (sor.ok() || unknown()) sor.value(); // [[unsafe]]
1546 }
1547 )cc");
1548 }
1549
1550 TEST_P(UncheckedStatusOrAccessModelTest, CorrelatedBranches) {
1551 ExpectDiagnosticsFor(R"cc(
1552 #include "unchecked_statusor_access_test_defs.h"
1553
1554 void target(bool b, STATUSOR_INT sor) {
1555 if (b || sor.ok()) {
1556 if (!b) sor.value();
1557 }
1558 }
1559 )cc");
1560 ExpectDiagnosticsFor(R"cc(
1561 #include "unchecked_statusor_access_test_defs.h"
1562
1563 void target(bool b, STATUSOR_INT sor) {
1564 if (b && !sor.ok()) return;
1565 if (b) sor.value();
1566 }
1567 )cc");
1568 ExpectDiagnosticsFor(R"cc(
1569 #include "unchecked_statusor_access_test_defs.h"
1570
1571 void target(bool b, STATUSOR_INT sor) {
1572 if (sor.ok()) b = true;
1573 if (b) sor.value(); // [[unsafe]]
1574 }
1575 )cc");
1576 ExpectDiagnosticsFor(R"cc(
1577 #include "unchecked_statusor_access_test_defs.h"
1578
1579 void target(bool b, STATUSOR_INT sor) {
1580 if (b) return;
1581 if (sor.ok()) b = true;
1582 if (b) sor.value();
1583 }
1584 )cc");
1585 }
1586
1587 TEST_P(UncheckedStatusOrAccessModelTest, ConditionWithInitStmt) {
1588 ExpectDiagnosticsFor(R"cc(
1589 #include "unchecked_statusor_access_test_defs.h"
1590
1591 void target() {
1592 if (STATUSOR_INT sor = Make<STATUSOR_INT>(); sor.ok()) sor.value();
1593 }
1594 )cc");
1595 ExpectDiagnosticsFor(R"cc(
1596 #include "unchecked_statusor_access_test_defs.h"
1597
1598 void target() {
1599 if (STATUSOR_INT sor = Make<STATUSOR_INT>(); !sor.ok())
1600 sor.value(); // [[unsafe]]
1601 }
1602 )cc");
1603 }
1604
1605 TEST_P(UncheckedStatusOrAccessModelTest, DeadCode) {
1606 ExpectDiagnosticsFor(R"cc(
1607 #include "unchecked_statusor_access_test_defs.h"
1608
1609 void target(STATUSOR_INT sor) {
1610 bool b = false;
1611 if (b) sor.value();
1612 }
1613 )cc");
1614 ExpectDiagnosticsFor(R"cc(
1615 #include "unchecked_statusor_access_test_defs.h"
1616
1617 void target(STATUSOR_INT sor) {
1618 bool b;
1619 b = false;
1620 if (b) sor.value();
1621 }
1622 )cc");
1623 }
1624
1625 TEST_P(UncheckedStatusOrAccessModelTest, TemporaryDestructors) {
1626 ExpectDiagnosticsFor(R"cc(
1627 #include "unchecked_statusor_access_test_defs.h"
1628
1629 void target(STATUSOR_INT sor) {
1630 sor.ok() ? sor.value() : Fatal().value();
1631
1632 sor.value();
1633 }
1634 )cc");
1635 ExpectDiagnosticsFor(R"cc(
1636 #include "unchecked_statusor_access_test_defs.h"
1637
1638 void target(STATUSOR_INT sor) {
1639 !sor.ok() ? Fatal().value() : sor.value();
1640
1641 sor.value();
1642 }
1643 )cc");
1644 ExpectDiagnosticsFor(R"cc(
1645 #include "unchecked_statusor_access_test_defs.h"
1646
1647 void target(bool b, STATUSOR_INT sor) {
1648 b ? 0 : sor.ok() ? sor.value() : Fatal().value();
1649
1650 sor.value(); // [[unsafe]]
1651 }
1652 )cc");
1653 ExpectDiagnosticsFor(R"cc(
1654 #include "unchecked_statusor_access_test_defs.h"
1655
1656 void target(bool b, STATUSOR_INT sor) {
1657 for (int i = 0; i < 10; i++) {
1658 (b && sor.ok()) ? sor.value() : Fatal().value();
1659
1660 if (b) sor.value();
1661 }
1662 }
1663 )cc");
1664 ExpectDiagnosticsFor(R"cc(
1665 #include "unchecked_statusor_access_test_defs.h"
1666
1667 void target(bool b, STATUSOR_INT sor) {
1668 for (int i = 0; i < 10; i++) {
1669 (b || !sor.ok()) ? Fatal().value() : 0;
1670
1671 if (!b) sor.value();
1672 }
1673 }
1674 )cc");
1675 ExpectDiagnosticsFor(R"cc(
1676 #include "unchecked_statusor_access_test_defs.h"
1677
1678 void target(bool b, STATUSOR_INT sor) {
1679 for (int i = 0; i < 10; i++) {
1680 (false || !(b && sor.ok())) ? Fatal().value() : 0;
1681
1682 do {
1683 sor.value();
1684 } while (b);
1685 }
1686 }
1687 )cc");
1688 }
1689
1690 TEST_P(UncheckedStatusOrAccessModelTest, CheckMacro) {
1691 ExpectDiagnosticsFor(R"cc(
1692 #include "unchecked_statusor_access_test_defs.h"
1693
1694 void target(STATUSOR_INT sor) {
1695 CHECK(sor.ok());
1696 sor.value();
1697 }
1698 )cc");
1699 ExpectDiagnosticsFor(R"cc(
1700 #include "unchecked_statusor_access_test_defs.h"
1701
1702 void target(STATUSOR_INT sor) {
1703 CHECK(!sor.ok());
1704 sor.value(); // [[unsafe]]
1705 }
1706 )cc");
1707 }
1708
1709 TEST_P(UncheckedStatusOrAccessModelTest, QcheckMacro) {
1710 ExpectDiagnosticsFor(R"cc(
1711 #include "unchecked_statusor_access_test_defs.h"
1712
1713 void target(STATUSOR_INT sor) {
1714 QCHECK(sor.ok());
1715 sor.value();
1716 }
1717 )cc");
1718 ExpectDiagnosticsFor(R"cc(
1719 #include "unchecked_statusor_access_test_defs.h"
1720
1721 void target(STATUSOR_INT sor) {
1722 QCHECK(!sor.ok());
1723 sor.value(); // [[unsafe]]
1724 }
1725 )cc");
1726 }
1727
1728 TEST_P(UncheckedStatusOrAccessModelTest, CheckOkMacro) {
1729 ExpectDiagnosticsFor(R"cc(
1730 #include "unchecked_statusor_access_test_defs.h"
1731
1732 void target(STATUSOR_INT sor) {
1733 CHECK_OK(sor.status());
1734 sor.value();
1735 }
1736 )cc");
1737 ExpectDiagnosticsFor(R"cc(
1738 #include "unchecked_statusor_access_test_defs.h"
1739
1740 void target(STATUSOR_INT sor) {
1741 CHECK_OK(sor);
1742 sor.value();
1743 }
1744 )cc");
1745 ExpectDiagnosticsFor(R"cc(
1746 #include "unchecked_statusor_access_test_defs.h"
1747
1748 void target() {
1749 STATUS s = Make<STATUS>();
1750 CHECK_OK(s);
1751 }
1752 )cc");
1753 }
1754
1755 TEST_P(UncheckedStatusOrAccessModelTest, QcheckOkMacro) {
1756 ExpectDiagnosticsFor(R"cc(
1757 #include "unchecked_statusor_access_test_defs.h"
1758
1759 void target(STATUSOR_INT sor) {
1760 QCHECK_OK(sor.status());
1761 sor.value();
1762 }
1763 )cc");
1764 ExpectDiagnosticsFor(R"cc(
1765 #include "unchecked_statusor_access_test_defs.h"
1766
1767 void target(STATUSOR_INT sor) {
1768 QCHECK_OK(sor);
1769 sor.value();
1770 }
1771 )cc");
1772 ExpectDiagnosticsFor(R"cc(
1773 #include "unchecked_statusor_access_test_defs.h"
1774
1775 void target() {
1776 STATUS s = Make<STATUS>();
1777 QCHECK_OK(s);
1778 }
1779 )cc");
1780 }
1781
1782 TEST_P(UncheckedStatusOrAccessModelTest, CheckEqMacro) {
1783 ExpectDiagnosticsFor(R"cc(
1784 #include "unchecked_statusor_access_test_defs.h"
1785
1786 void target(STATUSOR_INT sor) {
1787 CHECK_EQ(sor.status(), absl::OkStatus());
1788 sor.value();
1789 }
1790 )cc");
1791 ExpectDiagnosticsFor(R"cc(
1792 #include "unchecked_statusor_access_test_defs.h"
1793
1794 void target() {
1795 CHECK_EQ(Make<STATUS>(), absl::OkStatus());
1796 CHECK_EQ(absl::OkStatus(), Make<STATUS>());
1797 }
1798 )cc");
1799 }
1800
1801 TEST_P(UncheckedStatusOrAccessModelTest, QcheckEqMacro) {
1802 ExpectDiagnosticsFor(R"cc(
1803 #include "unchecked_statusor_access_test_defs.h"
1804
1805 void target(STATUSOR_INT sor) {
1806 QCHECK_EQ(sor.status(), absl::OkStatus());
1807 sor.value();
1808 }
1809 )cc");
1810 }
1811
1812 TEST_P(UncheckedStatusOrAccessModelTest, CheckNeMacro) {
1813 ExpectDiagnosticsFor(R"cc(
1814 #include "unchecked_statusor_access_test_defs.h"
1815
1816 void target(STATUSOR_INT sor) {
1817 CHECK_NE(sor.status(), absl::OkStatus());
1818 sor.value(); // [[unsafe]]
1819 }
1820 )cc");
1821 }
1822
1823 TEST_P(UncheckedStatusOrAccessModelTest, QcheckNeMacro) {
1824 ExpectDiagnosticsFor(R"cc(
1825 #include "unchecked_statusor_access_test_defs.h"
1826
1827 void target(STATUSOR_INT sor) {
1828 QCHECK_NE(sor.status(), absl::OkStatus());
1829 sor.value(); // [[unsafe]]
1830 }
1831 )cc");
1832 }
1833
1834 TEST_P(UncheckedStatusOrAccessModelTest, GlobalVars) {
1835 // The following examples are not sound as there could be opaque calls between
1836 // the ok() and the value() calls that change the StatusOr value.
1837 ExpectDiagnosticsFor(R"cc(
1838 #include "unchecked_statusor_access_test_defs.h"
1839
1840 static STATUSOR_INT sor;
1841
1842 void target() {
1843 if (sor.ok())
1844 sor.value();
1845 else
1846 sor.value(); // [[unsafe]]
1847 }
1848 )cc");
1849 ExpectDiagnosticsFor(R"cc(
1850 #include "unchecked_statusor_access_test_defs.h"
1851
1852 void target() {
1853 static STATUSOR_INT sor;
1854 if (sor.ok())
1855 sor.value();
1856 else
1857 sor.value(); // [[unsafe]]
1858 }
1859 )cc");
1860 ExpectDiagnosticsFor(R"cc(
1861 #include "unchecked_statusor_access_test_defs.h"
1862
1863 struct Foo {
1864 static STATUSOR_INT sor;
1865 };
1866
1867 void target(Foo foo) {
1868 if (foo.sor.ok())
1869 foo.sor.value();
1870 else
1871 foo.sor.value(); // [[unsafe]]
1872 }
1873 )cc");
1874 ExpectDiagnosticsFor(R"cc(
1875 #include "unchecked_statusor_access_test_defs.h"
1876
1877 struct Foo {
1878 static STATUSOR_INT sor;
1879 };
1880
1881 void target() {
1882 if (Foo::sor.ok())
1883 Foo::sor.value();
1884 else
1885 Foo::sor.value(); // [[unsafe]]
1886 }
1887 )cc");
1888 ExpectDiagnosticsFor(R"cc(
1889 #include "unchecked_statusor_access_test_defs.h"
1890
1891 struct Foo {
1892 static STATUSOR_INT sor;
1893
1894 static void target() {
1895 if (sor.ok())
1896 sor.value();
1897 else
1898 sor.value(); // [[unsafe]]
1899 }
1900 };
1901 )cc");
1902 ExpectDiagnosticsFor(R"cc(
1903 #include "unchecked_statusor_access_test_defs.h"
1904
1905 struct Foo {
1906 static STATUSOR_INT sor;
1907
1908 void target() {
1909 if (sor.ok())
1910 sor.value();
1911 else
1912 sor.value(); // [[unsafe]]
1913 }
1914 };
1915 )cc");
1916 ExpectDiagnosticsFor(R"cc(
1917 #include "unchecked_statusor_access_test_defs.h"
1918
1919 struct S {
1920 static const int x = -1;
1921 };
1922
1923 int target(S s) { return s.x; }
1924 )cc");
1925 }
1926
1927 TEST_P(UncheckedStatusOrAccessModelTest, ReferenceReceivers) {
1928 // The following examples are not sound as there could be opaque calls between
1929 // the ok() and the value() calls that change the StatusOr value. However,
1930 // this is the behavior that users expect so it is here to stay.
1931 ExpectDiagnosticsFor(R"cc(
1932 #include "unchecked_statusor_access_test_defs.h"
1933
1934 void target(STATUSOR_INT& sor) {
1935 if (sor.ok())
1936 sor.value();
1937 else
1938 sor.value(); // [[unsafe]]
1939 }
1940 )cc");
1941 ExpectDiagnosticsFor(R"cc(
1942 #include "unchecked_statusor_access_test_defs.h"
1943
1944 struct Foo {
1945 STATUSOR_INT& sor;
1946 };
1947
1948 void target(Foo foo) {
1949 if (foo.sor.ok())
1950 foo.sor.value();
1951 else
1952 foo.sor.value(); // [[unsafe]]
1953 }
1954 )cc");
1955 ExpectDiagnosticsFor(R"cc(
1956 #include "unchecked_statusor_access_test_defs.h"
1957
1958 struct Bar {
1959 STATUSOR_INT sor;
1960 };
1961
1962 struct Foo {
1963 Bar& bar;
1964 };
1965
1966 void target(Foo foo) {
1967 if (foo.bar.sor.ok())
1968 foo.bar.sor.value();
1969 else
1970 foo.bar.sor.value(); // [[unsafe]]
1971 }
1972 )cc");
1973 ExpectDiagnosticsFor(R"cc(
1974 #include "unchecked_statusor_access_test_defs.h"
1975
1976 struct Foo {
1977 STATUSOR_INT& sor;
1978 };
1979
1980 void target(Foo& foo) {
1981 if (foo.sor.ok())
1982 foo.sor.value();
1983 else
1984 foo.sor.value(); // [[unsafe]]
1985 }
1986 )cc");
1987 }
1988
1989 TEST_P(UncheckedStatusOrAccessModelTest, Lambdas) {
1990 ExpectDiagnosticsForLambda(R"cc(
1991 #include "unchecked_statusor_access_test_defs.h"
1992
1993 void target() {
1994 [](STATUSOR_INT sor) {
1995 if (sor.ok())
1996 sor.value();
1997 else
1998 sor.value(); // [[unsafe]]
1999 }(Make<STATUSOR_INT>());
2000 }
2001 )cc");
2002 ExpectDiagnosticsForLambda(R"cc(
2003 #include "unchecked_statusor_access_test_defs.h"
2004
2005 void target(STATUSOR_INT sor) {
2006 [sor]() {
2007 if (sor.ok())
2008 sor.value();
2009 else
2010 sor.value(); // [[unsafe]]
2011 }();
2012 }
2013 )cc");
2014 ExpectDiagnosticsForLambda(R"cc(
2015 #include "unchecked_statusor_access_test_defs.h"
2016
2017 void target(STATUSOR_INT sor) {
2018 [&sor]() {
2019 if (sor.ok())
2020 sor.value();
2021 else
2022 sor.value(); // [[unsafe]]
2023 }();
2024 }
2025 )cc");
2026 ExpectDiagnosticsForLambda(R"cc(
2027 #include "unchecked_statusor_access_test_defs.h"
2028
2029 void target(STATUSOR_INT sor) {
2030 [sor2 = sor]() {
2031 if (sor2.ok())
2032 sor2.value();
2033 else
2034 sor2.value(); // [[unsafe]]
2035 }();
2036 }
2037 )cc");
2038 ExpectDiagnosticsForLambda(R"cc(
2039 #include "unchecked_statusor_access_test_defs.h"
2040
2041 void target(STATUSOR_INT sor) {
2042 [&]() {
2043 if (sor.ok())
2044 sor.value();
2045 else
2046 sor.value(); // [[unsafe]]
2047 }();
2048 }
2049 )cc");
2050 ExpectDiagnosticsForLambda(R"cc(
2051 #include "unchecked_statusor_access_test_defs.h"
2052
2053 void target(STATUSOR_INT sor) {
2054 [=]() {
2055 if (sor.ok())
2056 sor.value();
2057 else
2058 sor.value(); // [[unsafe]]
2059 }();
2060 }
2061 )cc");
2062 ExpectDiagnosticsForLambda(R"cc(
2063 #include "unchecked_statusor_access_test_defs.h"
2064
2065 struct Foo {
2066 STATUSOR_INT sor;
2067
2068 void target() {
2069 [this]() {
2070 if (sor.ok())
2071 sor.value();
2072 else
2073 sor.value(); // [[unsafe]]
2074 }();
2075 }
2076 };
2077 )cc");
2078 }
2079
2080 TEST_P(UncheckedStatusOrAccessModelTest, GoodLambda) {
2081 ExpectDiagnosticsFor(R"cc(
2082 #include "unchecked_statusor_access_test_defs.h"
2083
2084 int target() {
2085 STATUSOR_INT sor = Make<STATUSOR_INT>();
2086 if (sor.ok()) return [&s = sor.value()] { return s; }();
2087 return 0;
2088 }
2089 )cc");
2090 }
2091
2092 TEST_P(UncheckedStatusOrAccessModelTest, Status) {
2093 ExpectDiagnosticsFor(R"cc(
2094 #include "unchecked_statusor_access_test_defs.h"
2095
2096 void foo();
2097
2098 void target(STATUS s) {
2099 if (s.ok()) foo();
2100 }
2101 )cc");
2102 ExpectDiagnosticsFor(R"cc(
2103 #include "unchecked_statusor_access_test_defs.h"
2104
2105 void foo();
2106
2107 void target() {
2108 STATUS s = Make<STATUSOR_INT>().status();
2109 if (s.ok()) foo();
2110 }
2111 )cc");
2112 }
2113
2114 TEST_P(UncheckedStatusOrAccessModelTest, ExpectThatMacro) {
2115 ExpectDiagnosticsFor(R"cc(
2116 #include "unchecked_statusor_access_test_defs.h"
2117
2118 void target(STATUSOR_INT sor) {
2119 EXPECT_THAT(sor, testing::status::IsOk());
2120
2121 sor.value(); // [[unsafe]]
2122 }
2123 )cc");
2124 ExpectDiagnosticsFor(R"cc(
2125 #include "unchecked_statusor_access_test_defs.h"
2126
2127 void target(STATUSOR_INT sor) {
2128 EXPECT_THAT(sor.status(), testing::status::IsOk());
2129
2130 sor.value(); // [[unsafe]]
2131 }
2132 )cc");
2133 ExpectDiagnosticsFor(R"cc(
2134 #include "unchecked_statusor_access_test_defs.h"
2135
2136 void target() {
2137 STATUSOR_INT sor = Make<STATUSOR_INT>();
2138 EXPECT_THAT(sor, testing::status::IsOk());
2139
2140 sor.value(); // [[unsafe]]
2141 }
2142 )cc");
2143 }
2144
2145 TEST_P(UncheckedStatusOrAccessModelTest, ExpectOkMacro) {
2146 ExpectDiagnosticsFor(R"cc(
2147 #include "unchecked_statusor_access_test_defs.h"
2148
2149 void target(STATUSOR_INT sor) {
2150 EXPECT_OK(sor);
2151
2152 sor.value(); // [[unsafe]]
2153 }
2154 )cc");
2155 ExpectDiagnosticsFor(R"cc(
2156 #include "unchecked_statusor_access_test_defs.h"
2157
2158 void target(STATUSOR_INT sor) {
2159 EXPECT_OK(sor.status());
2160
2161 sor.value(); // [[unsafe]]
2162 }
2163 )cc");
2164 ExpectDiagnosticsFor(R"cc(
2165 #include "unchecked_statusor_access_test_defs.h"
2166
2167 void target() {
2168 STATUSOR_INT sor = Make<STATUSOR_INT>();
2169 EXPECT_OK(sor);
2170
2171 sor.value(); // [[unsafe]]
2172 }
2173 )cc");
2174 }
2175
2176 TEST_P(UncheckedStatusOrAccessModelTest, AssertThatMacro) {
2177 ExpectDiagnosticsFor(R"cc(
2178 #include "unchecked_statusor_access_test_defs.h"
2179
2180 void target(STATUSOR_INT sor) {
2181 ASSERT_THAT(sor, testing::status::IsOk());
2182
2183 sor.value();
2184 }
2185 )cc");
2186 ExpectDiagnosticsFor(R"cc(
2187 #include "unchecked_statusor_access_test_defs.h"
2188
2189 void target(STATUSOR_INT sor) {
2190 ASSERT_THAT(sor, absl_testing::IsOk());
2191
2192 sor.value();
2193 }
2194 )cc");
2195 ExpectDiagnosticsFor(R"cc(
2196 #include "unchecked_statusor_access_test_defs.h"
2197
2198 void target(STATUSOR_INT sor) {
2199 ASSERT_THAT(sor.status(), testing::status::IsOk());
2200
2201 sor.value();
2202 }
2203 )cc");
2204 ExpectDiagnosticsFor(R"cc(
2205 #include "unchecked_statusor_access_test_defs.h"
2206 void target() {
2207 STATUSOR_INT sor = Make<STATUSOR_INT>();
2208 ASSERT_THAT(sor, testing::status::IsOk());
2209
2210 sor.value();
2211 }
2212 )cc");
2213 ExpectDiagnosticsFor(R"cc(
2214 #include "unchecked_statusor_access_test_defs.h"
2215
2216 void target(STATUSOR_INT sor) {
2217 using absl::StatusCode::kOk;
2218 ASSERT_THAT(sor, testing::status::StatusIs(kOk));
2219
2220 sor.value();
2221 }
2222 )cc");
2223 ExpectDiagnosticsFor(R"cc(
2224 #include "unchecked_statusor_access_test_defs.h"
2225
2226 void target(STATUSOR_INT sor) {
2227 ASSERT_THAT(sor, testing::status::StatusIs(absl::StatusCode::kOk));
2228
2229 sor.value();
2230 }
2231 )cc");
2232 ExpectDiagnosticsFor(R"cc(
2233 #include "unchecked_statusor_access_test_defs.h"
2234
2235 void target(STATUSOR_INT sor) {
2236 ASSERT_THAT(sor, testing::status::CanonicalStatusIs(absl::StatusCode::kOk));
2237
2238 sor.value();
2239 }
2240 )cc");
2241 ExpectDiagnosticsFor(R"cc(
2242 #include "unchecked_statusor_access_test_defs.h"
2243
2244 void target(STATUSOR_INT sor) {
2245 ASSERT_THAT(sor, absl_testing::CanonicalStatusIs(absl::StatusCode::kOk));
2246
2247 sor.value();
2248 }
2249 )cc");
2250 ExpectDiagnosticsFor(R"cc(
2251 #include "unchecked_statusor_access_test_defs.h"
2252
2253 void target(STATUSOR_INT sor) {
2254 auto matcher = testing::status::StatusIs(absl::StatusCode::kOk);
2255 ASSERT_THAT(sor, matcher);
2256
2257 sor.value();
2258 }
2259 )cc");
2260 ExpectDiagnosticsFor(R"cc(
2261 #include "unchecked_statusor_access_test_defs.h"
2262
2263 void target(STATUSOR_INT sor) {
2264 auto matcher = absl_testing::StatusIs(absl::StatusCode::kOk);
2265 ASSERT_THAT(sor, matcher);
2266
2267 sor.value();
2268 }
2269 )cc");
2270 ExpectDiagnosticsFor(R"cc(
2271 #include "unchecked_statusor_access_test_defs.h"
2272
2273 void target(STATUSOR_INT sor) {
2274 ASSERT_THAT(
2275 sor, testing::status::StatusIs(absl::StatusCode::kInvalidArgument));
2276
2277 sor.value(); // [[unsafe]]
2278 }
2279 )cc");
2280 ExpectDiagnosticsFor(R"cc(
2281 #include "unchecked_statusor_access_test_defs.h"
2282
2283 void target(STATUSOR_INT sor) {
2284 ASSERT_THAT(sor, absl_testing::StatusIs(absl::StatusCode::kInvalidArgument));
2285
2286 sor.value(); // [[unsafe]]
2287 }
2288 )cc");
2289
2290 ExpectDiagnosticsFor(R"cc(
2291 #include "unchecked_statusor_access_test_defs.h"
2292
2293 void target(STATUSOR_INT sor) {
2294 ASSERT_THAT(sor, testing::status::IsOkAndHolds(testing::IsTrue()));
2295
2296 sor.value();
2297 }
2298 )cc");
2299 ExpectDiagnosticsFor(R"cc(
2300 #include "unchecked_statusor_access_test_defs.h"
2301
2302 void target(STATUSOR_INT sor) {
2303 ASSERT_THAT(sor, absl_testing::IsOkAndHolds(testing::IsTrue()));
2304
2305 sor.value();
2306 }
2307 )cc");
2308 }
2309
2310 TEST_P(UncheckedStatusOrAccessModelTest, AssertTrueMacro) {
2311 ExpectDiagnosticsFor(R"cc(
2312 #include "unchecked_statusor_access_test_defs.h"
2313
2314 void target(STATUSOR_INT sor) {
2315 ASSERT_TRUE(sor.ok());
2316 sor.value();
2317 }
2318 )cc");
2319 ExpectDiagnosticsFor(R"cc(
2320 #include "unchecked_statusor_access_test_defs.h"
2321
2322 void target(STATUSOR_INT sor) {
2323 ASSERT_TRUE(sor.status().ok());
2324 sor.value();
2325 }
2326 )cc");
2327 ExpectDiagnosticsFor(R"cc(
2328 #include "unchecked_statusor_access_test_defs.h"
2329
2330 void target(STATUSOR_INT sor) {
2331 ASSERT_TRUE(!sor.ok());
2332 sor.value(); // [[unsafe]]
2333 }
2334 )cc");
2335 ExpectDiagnosticsFor(R"cc(
2336 #include "unchecked_statusor_access_test_defs.h"
2337
2338 void target() {
2339 bool flag = true;
2340
2341 ASSERT_TRUE(flag);
2342 "nop";
2343 }
2344 )cc");
2345
2346 ExpectDiagnosticsFor(R"cc(
2347 #include "unchecked_statusor_access_test_defs.h"
2348
2349 void target() {
2350 STATUSOR_BOOL flag = true;
2351
2352 ASSERT_TRUE(flag.value());
2353 "nop";
2354 }
2355 )cc");
2356 }
2357
2358 TEST_P(UncheckedStatusOrAccessModelTest, AssertOkMacro) {
2359 ExpectDiagnosticsFor(R"cc(
2360 #include "unchecked_statusor_access_test_defs.h"
2361
2362 void target(STATUSOR_INT sor) {
2363 ASSERT_OK(sor);
2364
2365 sor.value();
2366 }
2367 )cc");
2368 ExpectDiagnosticsFor(R"cc(
2369 #include "unchecked_statusor_access_test_defs.h"
2370
2371 void target(STATUSOR_INT sor) {
2372 ASSERT_OK(sor.status());
2373
2374 sor.value();
2375 }
2376 )cc");
2377 ExpectDiagnosticsFor(R"cc(
2378 #include "unchecked_statusor_access_test_defs.h"
2379 void target() {
2380 STATUSOR_INT sor = Make<STATUSOR_INT>();
2381 ASSERT_OK(sor);
2382
2383 sor.value();
2384 }
2385 )cc");
2386
2387 ExpectDiagnosticsFor(R"cc(
2388 #include "unchecked_statusor_access_test_defs.h"
2389
2390 void target(std::optional<STATUSOR_INT> sor_opt) {
2391 STATUSOR_INT sor = *sor_opt;
2392 ASSERT_OK(sor);
2393
2394 sor.value();
2395 }
2396 )cc");
2397 }
2398
2399 TEST_P(UncheckedStatusOrAccessModelTest, BreadthFirstBlockTraversalLoop) {
2400 // Evaluating the CFG blocks of the code below in breadth-first order results
2401 // in an infinite loop. Each iteration of the while loop below results in a
2402 // new value being assigned to the storage location of sor1. However,
2403 // following a bread-first order of evaluation, downstream blocks will join
2404 // environments of different generations of predecessor blocks having distinct
2405 // values assigned to the sotrage location of sor1, resulting in not assigning
2406 // a value to the storage location of sor1 in successors. As iterations of the
2407 // analysis go, the state of the environment flips between having a value
2408 // assigned to the storage location of sor1 and not having a value assigned to
2409 // it. Since the evaluation of the copy constructor expression in bar(sor1)
2410 // depends on a value being assigned to sor1, the state of the environment
2411 // also flips between having a storage location assigned to the bar(sor1)
2412 // expression and not having a storage location assigned to it. This leads to
2413 // an infinite loop as the environment can't stabilize.
2414 ExpectDiagnosticsFor(R"cc(
2415 #include "unchecked_statusor_access_test_defs.h"
2416
2417 void foo(int, int);
2418 STATUSOR_INT bar(STATUSOR_INT);
2419 void baz(int);
2420
2421 void target() {
2422 while (true) {
2423 STATUSOR_INT sor1 = Make<STATUSOR_INT>();
2424 if (sor1.ok()) {
2425 STATUSOR_INT sor2 = Make<STATUSOR_INT>();
2426 if (sor2.ok()) foo(sor1.value(), sor2.value());
2427 }
2428
2429 STATUSOR_INT sor3 = bar(sor1);
2430 for (int i = 0; i < 5; i++) sor3 = bar(sor1);
2431
2432 baz(sor3.value()); // [[unsafe]]
2433 }
2434 }
2435 )cc");
2436 }
2437
2438 TEST_P(UncheckedStatusOrAccessModelTest, ReturnValue) {
2439 ExpectDiagnosticsFor(R"cc(
2440 #include "unchecked_statusor_access_test_defs.h"
2441
2442 void target() {
2443 STATUSOR_INT sor = Make<STATUSOR_INT>();
2444 if (sor.ok())
2445 sor.value();
2446 else
2447 sor.value(); // [[unsafe]]
2448 }
2449 )cc");
2450 }
2451
2452 TEST_P(UncheckedStatusOrAccessModelTest, Goto) {
2453 ExpectDiagnosticsFor(R"cc(
2454 #include "unchecked_statusor_access_test_defs.h"
2455
2456 void target(STATUSOR_INT sor) {
2457 label:
2458 if (sor.ok())
2459 sor.value();
2460 else
2461 sor.value(); // [[unsafe]]
2462 goto label;
2463 }
2464 )cc");
2465 ExpectDiagnosticsFor(R"cc(
2466 #include "unchecked_statusor_access_test_defs.h"
2467
2468 void target(STATUSOR_INT sor) {
2469 label:
2470 if (!sor.ok()) goto label;
2471 sor.value();
2472 }
2473 )cc");
2474 ExpectDiagnosticsFor(R"cc(
2475 #include "unchecked_statusor_access_test_defs.h"
2476
2477 void target(STATUSOR_INT sor) {
2478 if (!sor.ok()) return;
2479 goto label;
2480 label:
2481 sor.value();
2482 }
2483 )cc");
2484 }
2485
2486 TEST_P(UncheckedStatusOrAccessModelTest, JoinDistinctValues) {
2487 ExpectDiagnosticsFor(R"cc(
2488 #include "unchecked_statusor_access_test_defs.h"
2489
2490 void target(bool b) {
2491 STATUSOR_INT sor;
2492 if (b)
2493 sor = Make<STATUSOR_INT>();
2494 else
2495 sor = Make<STATUSOR_INT>();
2496
2497 if (sor.ok())
2498 sor.value();
2499 else
2500 sor.value(); // [[unsafe]]
2501 }
2502 )cc");
2503 ExpectDiagnosticsFor(R"cc(
2504 #include "unchecked_statusor_access_test_defs.h"
2505
2506 void target(bool b) {
2507 STATUSOR_INT sor;
2508 if (b) {
2509 sor = Make<STATUSOR_INT>();
2510 if (!sor.ok()) return;
2511 } else {
2512 sor = Make<STATUSOR_INT>();
2513 if (!sor.ok()) return;
2514 }
2515 sor.value();
2516 }
2517 )cc");
2518 ExpectDiagnosticsFor(R"cc(
2519 #include "unchecked_statusor_access_test_defs.h"
2520
2521 void target(bool b) {
2522 STATUSOR_INT sor;
2523 if (b) {
2524 sor = Make<STATUSOR_INT>();
2525 if (!sor.ok()) return;
2526 } else {
2527 sor = Make<STATUSOR_INT>();
2528 }
2529 sor.value(); // [[unsafe]]
2530 }
2531 )cc");
2532 }
2533
2534 TEST_P(UncheckedStatusOrAccessModelTest, VarDeclInitExprFromPairAccess) {
2535 ExpectDiagnosticsFor(R"cc(
2536 #include "unchecked_statusor_access_test_defs.h"
2537
2538 void target() {
2539 auto sor = Make<std::pair<int, STATUSOR_INT>>().second;
2540 if (sor.ok())
2541 sor.value();
2542 else
2543 sor.value(); // [[unsafe]]
2544 }
2545 )cc");
2546 ExpectDiagnosticsFor(R"cc(
2547 #include "unchecked_statusor_access_test_defs.h"
2548
2549 void target() {
2550 const auto& sor = Make<std::pair<int, STATUSOR_INT>>().second;
2551 if (sor.ok())
2552 sor.value();
2553 else
2554 sor.value(); // [[unsafe]]
2555 }
2556 )cc");
2557 }
2558
2559 TEST_P(UncheckedStatusOrAccessModelTest, LValueToRValueCastOfChangingValue) {
2560 ExpectDiagnosticsFor(R"cc(
2561 #include "unchecked_statusor_access_test_defs.h"
2562
2563 bool foo();
2564
2565 void target(bool b1) {
2566 STATUSOR_INT sor;
2567 if (b1)
2568 sor = Make<STATUSOR_INT>();
2569 else
2570 sor = Make<STATUSOR_INT>();
2571
2572 do {
2573 const auto& b2 = foo();
2574 if (b2) break;
2575
2576 sor.value(); // [[unsafe]]
2577 } while (true);
2578 }
2579 )cc");
2580 }
2581
2582 TEST_P(UncheckedStatusOrAccessModelTest, ConstructorInitializer) {
2583 ExpectDiagnosticsFor(R"cc(
2584 #include "unchecked_statusor_access_test_defs.h"
2585
2586 class target {
2587 target() : foo_(Make<STATUSOR_INT>().value()) { // [[unsafe]]
2588 }
2589 int foo_;
2590 };
2591 )cc");
2592 }
2593
2594 TEST_P(UncheckedStatusOrAccessModelTest, AssignStatusToBoolVar) {
2595 ExpectDiagnosticsFor(R"cc(
2596 #include "unchecked_statusor_access_test_defs.h"
2597
2598 void target(STATUSOR_INT sor) {
2599 bool ok = sor.ok();
2600 if (ok)
2601 sor.value();
2602 else
2603 sor.value(); // [[unsafe]]
2604 }
2605 )cc");
2606 ExpectDiagnosticsFor(R"cc(
2607 #include "unchecked_statusor_access_test_defs.h"
2608
2609 void target(STATUSOR_INT sor) {
2610 bool not_ok = !sor.ok();
2611 if (not_ok)
2612 sor.value(); // [[unsafe]]
2613 else
2614 sor.value();
2615 }
2616 )cc");
2617 }
2618
2619 TEST_P(UncheckedStatusOrAccessModelTest, StructuredBindings) {
2620 // Binding to a pair (which is actually a struct in the mock header).
2621 ExpectDiagnosticsFor(R"cc(
2622 #include "unchecked_statusor_access_test_defs.h"
2623
2624 void target() {
2625 const auto [sor, x] = Make<std::pair<STATUSOR_INT, int>>();
2626 if (sor.ok()) sor.value();
2627 }
2628 )cc");
2629
2630 // Unsafe case.
2631 ExpectDiagnosticsFor(R"cc(
2632 #include "unchecked_statusor_access_test_defs.h"
2633
2634 void target() {
2635 const auto [sor, x] = Make<std::pair<STATUSOR_INT, int>>();
2636 sor.value(); // [[unsafe]]
2637 }
2638 )cc");
2639
2640 // As a reference.
2641 ExpectDiagnosticsFor(R"cc(
2642 #include "unchecked_statusor_access_test_defs.h"
2643
2644 void target() {
2645 const auto& [sor, x] = Make<std::pair<STATUSOR_INT, int>>();
2646 if (sor.ok()) sor.value();
2647 }
2648 )cc");
2649
2650 // Binding to a ref in a struct.
2651 ExpectDiagnosticsFor(R"cc(
2652 #include "unchecked_statusor_access_test_defs.h"
2653
2654 struct S {
2655 STATUSOR_INT& sor;
2656 int i;
2657 };
2658
2659 void target() {
2660 const auto& [sor, i] = Make<S>();
2661 if (sor.ok()) sor.value();
2662 }
2663 )cc");
2664
2665 // In a loop.
2666 ExpectDiagnosticsFor(R"cc(
2667 #include "unchecked_statusor_access_test_defs.h"
2668
2669 void target() {
2670 auto vals = Make<std::vector<std::pair<int, STATUSOR_INT>>>();
2671 for (const auto& [x, sor] : vals)
2672 if (sor.ok()) sor.value();
2673 }
2674 )cc");
2675
2676 // Similar to the above, but InitExpr already has the storage initialized,
2677 // and bindings refer to them.
2678 ExpectDiagnosticsFor(R"cc(
2679 #include "unchecked_statusor_access_test_defs.h"
2680
2681 void target() {
2682 auto vals = Make<std::vector<std::pair<int, STATUSOR_INT>>>();
2683 for (const auto& p : vals) {
2684 const auto& [i, sor] = p;
2685 if (sor.ok()) sor.value();
2686 }
2687 }
2688 )cc");
2689 }
2690
2691 TEST_P(UncheckedStatusOrAccessModelTest, AssignCompositeLogicExprToVar) {
2692 ExpectDiagnosticsFor(
2693 R"cc(
2694 #include "unchecked_statusor_access_test_defs.h"
2695
2696 void target(STATUSOR_INT sor, bool b) {
2697 bool c = sor.ok() && b;
2698 if (c) sor.value();
2699 }
2700 )cc");
2701
2702 ExpectDiagnosticsFor(
2703 R"cc(
2704 #include "unchecked_statusor_access_test_defs.h"
2705
2706 void target(STATUSOR_INT sor, bool b) {
2707 bool c = !(!sor.ok() || !b);
2708 if (c) sor.value();
2709 }
2710 )cc");
2711 }
2712
2713 TEST_P(UncheckedStatusOrAccessModelTest, Subclass) {
2714 ExpectDiagnosticsFor(
2715 R"cc(
2716 #include "unchecked_statusor_access_test_defs.h"
2717
2718 class Foo : public STATUSOR_INT {};
2719
2720 void target(Foo opt) {
2721 opt.value(); // [[unsafe]]
2722 }
2723 )cc");
2724 }
2725
2726 TEST_P(UncheckedStatusOrAccessModelTest, SubclassStatus) {
2727 ExpectDiagnosticsFor(
2728 R"cc(
2729 #include "unchecked_statusor_access_test_defs.h"
2730
2731 class Foo : public STATUS {};
2732
2733 void target(Foo opt) { opt.ok(); }
2734 )cc");
2735 }
2736
2737 TEST_P(UncheckedStatusOrAccessModelTest, SubclassOk) {
2738 ExpectDiagnosticsFor(
2739 R"cc(
2740 #include "unchecked_statusor_access_test_defs.h"
2741
2742 class Foo : public STATUSOR_INT {};
2743
2744 void target(Foo opt) {
2745 if (opt.ok()) opt.value();
2746 }
2747 )cc");
2748 }
2749
2750 TEST_P(UncheckedStatusOrAccessModelTest, SubclassOperator) {
2751 ExpectDiagnosticsFor(
2752 R"cc(
2753 #include "unchecked_statusor_access_test_defs.h"
2754
2755 class Foo : public STATUSOR_INT {};
2756
2757 void target(Foo opt) {
2758 *opt; // [[unsafe]]
2759 }
2760 )cc");
2761 }
2762
2763 TEST_P(UncheckedStatusOrAccessModelTest, UnwrapValueWithStatusCheck) {
2764 ExpectDiagnosticsFor(R"cc(
2765 #include "unchecked_statusor_access_test_defs.h"
2766
2767 void target(STATUSOR_INT sor) {
2768 if (sor.status().ok())
2769 sor.value();
2770 else
2771 sor.value(); // [[unsafe]]
2772 }
2773 )cc");
2774 }
2775
2776 TEST_P(UncheckedStatusOrAccessModelTest, UnwrapValueWithStatusRefCheck) {
2777 ExpectDiagnosticsFor(R"cc(
2778 #include "unchecked_statusor_access_test_defs.h"
2779
2780 void target(STATUSOR_INT sor) {
2781 const STATUS& s = sor.status();
2782 if (s.ok())
2783 sor.value();
2784 else
2785 sor.value(); // [[unsafe]]
2786 }
2787 )cc");
2788 }
2789
2790 TEST_P(UncheckedStatusOrAccessModelTest, UnwrapValueWithStatusPtrCheck) {
2791 ExpectDiagnosticsFor(R"cc(
2792 #include "unchecked_statusor_access_test_defs.h"
2793
2794 void target(STATUSOR_INT sor) {
2795 const STATUS* s = &sor.status();
2796 if (s->ok())
2797 sor.value();
2798 else
2799 sor.value(); // [[unsafe]]
2800 }
2801 )cc");
2802 }
2803
2804 TEST_P(UncheckedStatusOrAccessModelTest, UnwrapValueWithMovedStatus) {
2805 ExpectDiagnosticsFor(R"cc(
2806 #include "unchecked_statusor_access_test_defs.h"
2807
2808 void target(STATUSOR_INT sor) {
2809 if (std::move(sor.status()).ok())
2810 sor.value();
2811 else
2812 sor.value(); // [[unsafe]]
2813 }
2814 )cc");
2815 }
2816
2817 TEST_P(UncheckedStatusOrAccessModelTest, MembersUsedInsideStatus) {
2818 ExpectDiagnosticsFor(R"cc(
2819 namespace absl {
2820
2821 class Status {
2822 public:
2823 bool ok() const;
2824
2825 void target() const { ok(); }
2826 };
2827
2828 } // namespace absl
2829 )cc");
2830 }
2831
2832 TEST_P(UncheckedStatusOrAccessModelTest, StatusUpdate) {
2833 ExpectDiagnosticsFor(R"cc(
2834 #include "unchecked_statusor_access_test_defs.h"
2835
2836 void target(STATUSOR_INT sor) {
2837 STATUS s;
2838 s.Update(sor.status());
2839 if (s.ok())
2840 sor.value();
2841 else
2842 sor.value(); // [[unsafe]]
2843 }
2844 )cc");
2845
2846 ExpectDiagnosticsFor(R"cc(
2847 #include "unchecked_statusor_access_test_defs.h"
2848
2849 void target(STATUSOR_INT sor1, STATUSOR_INT sor2) {
2850 STATUS s;
2851 s.Update(sor1.status());
2852 s.Update(sor2.status());
2853 if (s.ok()) {
2854 sor1.value();
2855 sor2.value();
2856 }
2857 }
2858 )cc");
2859
2860 ExpectDiagnosticsFor(R"cc(
2861 #include "unchecked_statusor_access_test_defs.h"
2862
2863 void target(STATUSOR_INT sor1, STATUSOR_INT sor2) {
2864 STATUS s;
2865 s.Update(sor1.status());
2866 CHECK(s.ok());
2867 s.Update(sor2.status());
2868 sor1.value();
2869 sor2.value(); // [[unsafe]]
2870 }
2871 )cc");
2872
2873 ExpectDiagnosticsFor(R"cc(
2874 #include "unchecked_statusor_access_test_defs.h"
2875
2876 void target(STATUSOR_INT sor1, STATUSOR_INT sor2) {
2877 STATUS s;
2878 s.Update(sor1.status());
2879 CHECK(s.ok());
2880 sor1.value();
2881 sor2.value(); // [[unsafe]]
2882 }
2883 )cc");
2884
2885 ExpectDiagnosticsFor(R"cc(
2886 #include "unchecked_statusor_access_test_defs.h"
2887
2888 void target(STATUSOR_INT sor1, STATUSOR_INT sor2) {
2889 STATUS s;
2890 STATUS sor1_status = sor1.status();
2891 s.Update(std::move(sor1_status));
2892 CHECK(s.ok());
2893 sor1.value();
2894 sor2.value(); // [[unsafe]]
2895 }
2896 )cc");
2897
2898 ExpectDiagnosticsFor(R"cc(
2899 #include "unchecked_statusor_access_test_defs.h"
2900
2901 void target(STATUSOR_INT sor1, STATUSOR_INT sor2) {
2902 STATUS s;
2903 STATUS sor1_status = sor1.status();
2904 sor1_status.Update(sor2.status());
2905 s.Update(std::move(sor1_status));
2906 CHECK(s.ok());
2907 sor1.value();
2908 sor2.value();
2909 }
2910 )cc");
2911 ExpectDiagnosticsFor(R"cc(
2912 #include "unchecked_statusor_access_test_defs.h"
2913
2914 const STATUS& OptStatus();
2915
2916 void target(STATUSOR_INT sor) {
2917 auto s = sor.status();
2918 s.Update(OptStatus());
2919 if (s.ok()) sor.value();
2920 }
2921 )cc");
2922 }
2923
2924 TEST_P(UncheckedStatusOrAccessModelTest, EqualityCheck) {
2925 ExpectDiagnosticsFor(
2926 R"cc(
2927 #include "unchecked_statusor_access_test_defs.h"
2928
2929 void target(STATUSOR_INT x, STATUSOR_INT y) {
2930 if (x.ok()) {
2931 if (x == y)
2932 y.value();
2933 else
2934 y.value(); // [[unsafe]]
2935 }
2936 }
2937 )cc");
2938 ExpectDiagnosticsFor(
2939 R"cc(
2940 #include "unchecked_statusor_access_test_defs.h"
2941
2942 void target(STATUSOR_INT x, STATUSOR_INT y) {
2943 if (x.ok()) {
2944 if (y == x)
2945 y.value();
2946 else
2947 y.value(); // [[unsafe]]
2948 }
2949 }
2950 )cc");
2951 ExpectDiagnosticsFor(R"cc(
2952 #include "unchecked_statusor_access_test_defs.h"
2953
2954 void target(STATUSOR_INT x, STATUSOR_INT y) {
2955 if (x.ok()) {
2956 if (x != y)
2957 y.value(); // [[unsafe]]
2958 else
2959 y.value();
2960 }
2961 }
2962 )cc");
2963 ExpectDiagnosticsFor(R"cc(
2964 #include "unchecked_statusor_access_test_defs.h"
2965
2966 void target(STATUSOR_INT x, STATUSOR_INT y) {
2967 if (x.ok()) {
2968 if (y != x)
2969 y.value(); // [[unsafe]]
2970 else
2971 y.value();
2972 }
2973 }
2974 )cc");
2975 ExpectDiagnosticsFor(R"cc(
2976 #include "unchecked_statusor_access_test_defs.h"
2977
2978 void target(STATUSOR_INT x, STATUSOR_INT y) {
2979 if (x.ok()) {
2980 if (!(x == y))
2981 y.value(); // [[unsafe]]
2982 else
2983 y.value();
2984 }
2985 }
2986 )cc");
2987 ExpectDiagnosticsFor(
2988 R"cc(
2989 #include "unchecked_statusor_access_test_defs.h"
2990
2991 void target(STATUSOR_INT x, STATUSOR_INT y) {
2992 if (x.ok()) {
2993 if (!(x != y))
2994 y.value();
2995 else
2996 y.value(); // [[unsafe]]
2997 }
2998 }
2999 )cc");
3000 ExpectDiagnosticsFor(R"cc(
3001 #include "unchecked_statusor_access_test_defs.h"
3002
3003 void target(STATUSOR_INT x, STATUSOR_INT y) {
3004 if (x == y)
3005 if (x.ok()) y.value();
3006 }
3007 )cc");
3008 ExpectDiagnosticsFor(
3009 R"cc(
3010 #include "unchecked_statusor_access_test_defs.h"
3011
3012 void target(STATUSOR_INT x, STATUSOR_INT y) {
3013 if (x.ok()) {
3014 if (x.status() == y.status())
3015 y.value();
3016 else
3017 y.value(); // [[unsafe]]
3018 }
3019 }
3020 )cc");
3021 ExpectDiagnosticsFor(R"cc(
3022 #include "unchecked_statusor_access_test_defs.h"
3023
3024 void target(STATUSOR_INT x, STATUSOR_INT y) {
3025 if (x.ok()) {
3026 if (x.status() != y.status())
3027 y.value(); // [[unsafe]]
3028 else
3029 y.value();
3030 }
3031 }
3032 )cc");
3033 ExpectDiagnosticsFor(R"cc(
3034 #include "unchecked_statusor_access_test_defs.h"
3035
3036 void target(STATUSOR_INT sor) {
3037 if (sor.status() == absl::OkStatus())
3038 sor.value();
3039 else
3040 sor.value(); // [[unsafe]]
3041 }
3042 )cc");
3043 ExpectDiagnosticsFor(R"cc(
3044 #include "unchecked_statusor_access_test_defs.h"
3045
3046 void target(STATUSOR_INT sor) {
3047 if (sor.status() != absl::OkStatus())
3048 sor.value(); // [[unsafe]]
3049 else
3050 sor.value();
3051 }
3052 )cc");
3053 ExpectDiagnosticsFor(R"cc(
3054 #include "unchecked_statusor_access_test_defs.h"
3055
3056 void target(STATUSOR_INT sor) {
3057 if (sor.status() != absl::InvalidArgumentError("oh no"))
3058 sor.value(); // [[unsafe]]
3059 else
3060 sor.value(); // [[unsafe]]
3061 }
3062 )cc");
3063 ExpectDiagnosticsFor(
3064 R"cc(
3065 #include "unchecked_statusor_access_test_defs.h"
3066
3067 void target(STATUSOR_INT x, STATUSOR_INT y) {
3068 if (x.ok()) {
3069 if (x.ok() == y.ok())
3070 y.value();
3071 else
3072 y.value(); // [[unsafe]]
3073 }
3074 }
3075 )cc");
3076 ExpectDiagnosticsFor(R"cc(
3077 #include "unchecked_statusor_access_test_defs.h"
3078
3079 void target(STATUSOR_INT x, STATUSOR_INT y) {
3080 if (x.ok()) {
3081 if (x.ok() != y.ok())
3082 y.value(); // [[unsafe]]
3083 else
3084 y.value();
3085 }
3086 }
3087 )cc");
3088 ExpectDiagnosticsFor(
3089 R"cc(
3090 #include "unchecked_statusor_access_test_defs.h"
3091
3092 void target(STATUSOR_INT x, STATUSOR_INT y) {
3093 if (x.ok()) {
3094 if (x.status().ok() == y.status().ok())
3095 y.value();
3096 else
3097 y.value(); // [[unsafe]]
3098 }
3099 }
3100 )cc");
3101 ExpectDiagnosticsFor(R"cc(
3102 #include "unchecked_statusor_access_test_defs.h"
3103
3104 void target(STATUSOR_INT x, STATUSOR_INT y) {
3105 if (x.ok()) {
3106 if (x.status().ok() != y.status().ok())
3107 y.value(); // [[unsafe]]
3108 else
3109 y.value();
3110 }
3111 }
3112 )cc");
3113 ExpectDiagnosticsFor(
3114 R"cc(
3115 #include "unchecked_statusor_access_test_defs.h"
3116
3117 void target(STATUSOR_INT x, STATUSOR_INT y) {
3118 if (x.ok()) {
3119 if (x.status().ok() == y.ok())
3120 y.value();
3121 else
3122 y.value(); // [[unsafe]]
3123 }
3124 }
3125 )cc");
3126 ExpectDiagnosticsFor(R"cc(
3127 #include "unchecked_statusor_access_test_defs.h"
3128
3129 void target(STATUSOR_INT x, STATUSOR_INT y) {
3130 if (x.ok()) {
3131 if (x.status().ok() != y.ok())
3132 y.value(); // [[unsafe]]
3133 else
3134 y.value();
3135 }
3136 }
3137 )cc");
3138 ExpectDiagnosticsFor(R"cc(
3139 #include "unchecked_statusor_access_test_defs.h"
3140
3141 void target(bool b, STATUSOR_INT sor) {
3142 if (sor.ok() == b) {
3143 if (b) sor.value();
3144 }
3145 }
3146 )cc");
3147 ExpectDiagnosticsFor(R"cc(
3148 #include "unchecked_statusor_access_test_defs.h"
3149
3150 void target(STATUSOR_INT sor) {
3151 if (sor.ok() == true) sor.value();
3152 }
3153 )cc");
3154 ExpectDiagnosticsFor(R"cc(
3155 #include "unchecked_statusor_access_test_defs.h"
3156
3157 void target(STATUSOR_INT sor) {
3158 if (sor.ok() == false) sor.value(); // [[unsafe]]
3159 }
3160 )cc");
3161 ExpectDiagnosticsFor(R"cc(
3162 #include "unchecked_statusor_access_test_defs.h"
3163
3164 void target(bool b) {
3165 STATUSOR_INT sor1;
3166 STATUSOR_INT sor2 = Make<STATUSOR_INT>();
3167 if (sor1 == sor2) sor2.value(); // [[unsafe]]
3168 }
3169 )cc");
3170 ExpectDiagnosticsFor(R"cc(
3171 #include "unchecked_statusor_access_test_defs.h"
3172
3173 void target(bool b) {
3174 STATUSOR_INT sor1 = Make<STATUSOR_INT>();
3175 STATUSOR_INT sor2;
3176 if (sor1 == sor2) sor1.value(); // [[unsafe]]
3177 }
3178 )cc");
3179 }
3180
3181 TEST_P(UncheckedStatusOrAccessModelTest, PointerEqualityCheck) {
3182 ExpectDiagnosticsFor(
3183 R"cc(
3184 #include "unchecked_statusor_access_test_defs.h"
3185
3186 void target(STATUSOR_INT* x, STATUSOR_INT* y) {
3187 if (x->ok()) {
3188 if (x == y)
3189 y->value();
3190 else
3191 y->value(); // [[unsafe]]
3192 }
3193 }
3194 )cc");
3195 ExpectDiagnosticsFor(
3196 R"cc(
3197 #include "unchecked_statusor_access_test_defs.h"
3198
3199 void target(STATUSOR_INT* x, STATUSOR_INT* y) {
3200 if (x->ok()) {
3201 if (x != y)
3202 y->value(); // [[unsafe]]
3203 else
3204 y->value();
3205 }
3206 }
3207 )cc");
3208 ExpectDiagnosticsFor(
3209 R"cc(
3210 #include "unchecked_statusor_access_test_defs.h"
3211
3212 void target(STATUS* x, STATUS* y) {
3213 auto sor = Make<STATUSOR_INT>();
3214 if (x->ok()) {
3215 if (x == y && sor.status() == *y)
3216 sor.value();
3217 else
3218 sor.value(); // [[unsafe]]
3219 }
3220 }
3221 )cc");
3222 ExpectDiagnosticsFor(
3223 R"cc(
3224 #include "unchecked_statusor_access_test_defs.h"
3225
3226 void target(STATUS* x, STATUS* y) {
3227 auto sor = Make<STATUSOR_INT>();
3228 if (x->ok()) {
3229 if (x != y)
3230 sor.value(); // [[unsafe]]
3231 else if (sor.status() == *y)
3232 sor.value();
3233 }
3234 }
3235 )cc");
3236 }
3237
3238 TEST_P(UncheckedStatusOrAccessModelTest, Emplace) {
3239 ExpectDiagnosticsFor(R"cc(
3240 #include "unchecked_statusor_access_test_defs.h"
3241
3242 struct Foo {
3243 Foo(int);
3244 };
3245
3246 void target(absl::StatusOr<Foo> sor, int value) {
3247 sor.emplace(value);
3248 sor.value();
3249 }
3250 )cc");
3251 ExpectDiagnosticsFor(R"cc(
3252 #include "unchecked_statusor_access_test_defs.h"
3253
3254 struct Foo {
3255 Foo(std::initializer_list<int>, int);
3256 };
3257
3258 void target(absl::StatusOr<Foo> sor, int value) {
3259 sor.emplace({1, 2, 3}, value);
3260 sor.value();
3261 }
3262 )cc");
3263 ExpectDiagnosticsFor(R"cc(
3264 #include "unchecked_statusor_access_test_defs.h"
3265
3266 void target() {
3267 STATUSOR_INT sor;
3268 bool sor_ok = sor.ok();
3269 if (!sor_ok)
3270 sor.emplace(42);
3271 sor.value();
3272 }
3273 )cc");
3274 ExpectDiagnosticsFor(R"cc(
3275 #include "unchecked_statusor_access_test_defs.h"
3276
3277 void target(bool b) {
3278 STATUSOR_INT sor;
3279 if (b) sor.emplace(42);
3280 if (b) sor.value();
3281 }
3282 )cc");
3283 }
3284
3285 TEST_P(UncheckedStatusOrAccessModelTest, ValueConstruction) {
3286 ExpectDiagnosticsFor(R"cc(
3287 #include "unchecked_statusor_access_test_defs.h"
3288
3289 void target() {
3290 STATUSOR_BOOL result = false;
3291 result.value();
3292 }
3293 )cc");
3294 ExpectDiagnosticsFor(R"cc(
3295 #include "unchecked_statusor_access_test_defs.h"
3296
3297 void target() {
3298 STATUSOR_INT result = 21;
3299 result.value();
3300 }
3301 )cc");
3302 ExpectDiagnosticsFor(R"cc(
3303 #include "unchecked_statusor_access_test_defs.h"
3304
3305 void target() {
3306 STATUSOR_INT result = Make<STATUSOR_INT>();
3307 result.value(); // [[unsafe]]
3308 }
3309 )cc");
3310 ExpectDiagnosticsFor(
3311 R"cc(
3312 #include "unchecked_statusor_access_test_defs.h"
3313
3314 void target() {
3315 STATUSOR_BOOL result = false;
3316 if (result.ok())
3317 result.value();
3318 else
3319 result.value();
3320 }
3321 )cc");
3322
3323 ExpectDiagnosticsFor(R"cc(
3324 #include "unchecked_statusor_access_test_defs.h"
3325
3326 void target() {
3327 STATUSOR_BOOL result(false);
3328 result.value();
3329 }
3330 )cc");
3331 ExpectDiagnosticsFor(R"cc(
3332 #include "unchecked_statusor_access_test_defs.h"
3333
3334 void target() {
3335 STATUSOR_INT result(21);
3336 result.value();
3337 }
3338 )cc");
3339 ExpectDiagnosticsFor(R"cc(
3340 #include "unchecked_statusor_access_test_defs.h"
3341
3342 void target() {
3343 STATUSOR_INT result(Make<STATUSOR_INT>());
3344 result.value(); // [[unsafe]]
3345 }
3346 )cc");
3347 ExpectDiagnosticsFor(
3348 R"cc(
3349 #include "unchecked_statusor_access_test_defs.h"
3350
3351 void target() {
3352 STATUSOR_BOOL result(false);
3353 if (result.ok())
3354 result.value();
3355 else
3356 result.value();
3357 }
3358 )cc");
3359 }
3360
3361 TEST_P(UncheckedStatusOrAccessModelTest, ValueAssignment) {
3362 ExpectDiagnosticsFor(R"cc(
3363 #include "unchecked_statusor_access_test_defs.h"
3364
3365 void target() {
3366 STATUSOR_BOOL result;
3367 result = false;
3368 result.value();
3369 }
3370 )cc");
3371 ExpectDiagnosticsFor(R"cc(
3372 #include "unchecked_statusor_access_test_defs.h"
3373
3374 void target() {
3375 STATUSOR_INT result;
3376 result = 21;
3377 result.value();
3378 }
3379 )cc");
3380 ExpectDiagnosticsFor(R"cc(
3381 #include "unchecked_statusor_access_test_defs.h"
3382
3383 void target() {
3384 STATUSOR_INT result;
3385 result = Make<STATUSOR_INT>();
3386 result.value(); // [[unsafe]]
3387 }
3388 )cc");
3389 ExpectDiagnosticsFor(
3390 R"cc(
3391 #include "unchecked_statusor_access_test_defs.h"
3392
3393 void target() {
3394 STATUSOR_BOOL result;
3395 result = false;
3396 if (result.ok())
3397 result.value();
3398 else
3399 result.value();
3400 }
3401 )cc");
3402 }
3403
3404 TEST_P(UncheckedStatusOrAccessModelTest, NestedStatusOr) {
3405 ExpectDiagnosticsFor(R"cc(
3406 #include "unchecked_statusor_access_test_defs.h"
3407
3408 void target() {
3409 absl::StatusOr<STATUSOR_INT> result;
3410 result = Make<STATUSOR_INT>();
3411 result.value();
3412 }
3413 )cc");
3414 ExpectDiagnosticsFor(R"cc(
3415 #include "unchecked_statusor_access_test_defs.h"
3416
3417 void target() {
3418 absl::StatusOr<STATUSOR_INT> result = Make<STATUSOR_INT>();
3419 result.value();
3420 }
3421 )cc");
3422 ExpectDiagnosticsFor(R"cc(
3423 #include "unchecked_statusor_access_test_defs.h"
3424
3425 void target() {
3426 absl::StatusOr<STATUSOR_INT> result = Make<STATUSOR_INT>();
3427 if (result.value().ok())
3428 result.value().value();
3429 }
3430 )cc");
3431 }
3432
3433 TEST_P(UncheckedStatusOrAccessModelTest, PtrConstruct) {
3434 ExpectDiagnosticsFor(R"cc(
3435 #include "unchecked_statusor_access_test_defs.h"
3436
3437 void target() {
3438 STATUSOR_VOIDPTR sor = nullptr;
3439 *sor;
3440 }
3441 )cc");
3442
3443 ExpectDiagnosticsFor(R"cc(
3444 #include "unchecked_statusor_access_test_defs.h"
3445
3446 void target() {
3447 STATUSOR_VOIDPTR sor(nullptr);
3448 *sor;
3449 }
3450 )cc");
3451 }
3452
3453 TEST_P(UncheckedStatusOrAccessModelTest, InPlaceConstruct) {
3454 ExpectDiagnosticsFor(R"cc(
3455 #include "unchecked_statusor_access_test_defs.h"
3456
3457 void target() {
3458 STATUSOR_VOIDPTR absl_sor(absl::in_place, {nullptr});
3459 *absl_sor;
3460 STATUSOR_VOIDPTR std_sor(std::in_place, {nullptr});
3461 *std_sor;
3462 }
3463 )cc");
3464 }
3465
3466 TEST_P(UncheckedStatusOrAccessModelTest, ConstructStatusOrFromReference) {
3467 ExpectDiagnosticsFor(R"cc(
3468 #include "unchecked_statusor_access_test_defs.h"
3469 void target() {
3470 const auto sor1 = Make<STATUSOR_INT&>();
3471 const auto sor2 = Make<STATUSOR_INT&>();
3472 if (!sor1.ok() && !sor2.ok()) return;
3473 if (sor1.ok() && !sor2.ok()) {
3474 } else if (!sor1.ok() && sor2.ok()) {
3475 } else {
3476 sor1.value();
3477 sor2.value();
3478 }
3479 }
3480 )cc");
3481 }
3482
3483 TEST_P(UncheckedStatusOrAccessModelTest, ConstructStatusFromReference) {
3484 ExpectDiagnosticsFor(R"cc(
3485 #include "unchecked_statusor_access_test_defs.h"
3486
3487 void target() {
3488 const auto sor1 = Make<STATUSOR_INT&>();
3489 const auto sor2 = Make<STATUSOR_INT&>();
3490 const auto s1 = Make<STATUS&>();
3491 const auto s2 = Make<STATUS&>();
3492
3493 if (!s1.ok() && !s2.ok()) return;
3494 if (s1.ok() && !s2.ok()) {
3495 } else if (!s1.ok() && s2.ok()) {
3496 } else {
3497 if (s1 != sor1.status() || s2 != sor2.status()) return;
3498 sor1.value();
3499 sor2.value();
3500 }
3501 }
3502 )cc");
3503 }
3504
3505 TEST_P(UncheckedStatusOrAccessModelTest, AccessorCall) {
3506 // Accessor returns reference.
3507 ExpectDiagnosticsFor(
3508 R"cc(
3509 #include "unchecked_statusor_access_test_defs.h"
3510
3511 struct Foo {
3512 STATUSOR_INT sor_;
3513
3514 const STATUSOR_INT& sor() const { return sor_; }
3515 };
3516
3517 void target(Foo foo) {
3518 if (foo.sor().ok()) foo.sor().value();
3519 }
3520 )cc");
3521
3522 // Uses an operator
3523 ExpectDiagnosticsFor(
3524 R"cc(
3525 #include "unchecked_statusor_access_test_defs.h"
3526
3527 struct Foo {
3528 STATUSOR_INT sor_;
3529
3530 const STATUSOR_INT& operator()() const { return sor_; }
3531 };
3532
3533 void target(Foo foo) {
3534 if (foo().ok()) foo().value();
3535 }
3536 )cc");
3537
3538 // Calls nonconst method in between.
3539 ExpectDiagnosticsFor(
3540 R"cc(
3541 #include "unchecked_statusor_access_test_defs.h"
3542
3543 struct Foo {
3544 STATUSOR_INT sor_;
3545
3546 void invalidate() {}
3547
3548 const STATUSOR_INT& sor() const { return sor_; }
3549 };
3550
3551 void target(Foo foo) {
3552 if (foo.sor().ok()) {
3553 foo.invalidate();
3554 foo.sor().value(); // [[unsafe]]
3555 }
3556 }
3557 )cc");
3558
3559 // Calls nonconst operator in between.
3560 ExpectDiagnosticsFor(
3561 R"cc(
3562 #include "unchecked_statusor_access_test_defs.h"
3563
3564 struct Foo {
3565 STATUSOR_INT sor_;
3566
3567 void operator()() {}
3568
3569 const STATUSOR_INT& sor() const { return sor_; }
3570 };
3571
3572 void target(Foo foo) {
3573 if (foo.sor().ok()) {
3574 foo();
3575 foo.sor().value(); // [[unsafe]]
3576 }
3577 }
3578 )cc");
3579
3580 // Accessor returns copy.
3581 ExpectDiagnosticsFor(
3582 R"cc(
3583 #include "unchecked_statusor_access_test_defs.h"
3584
3585 struct Foo {
3586 STATUSOR_INT sor_;
3587
3588 STATUSOR_INT sor() const { return sor_; }
3589 };
3590
3591 void target(Foo foo) {
3592 if (foo.sor().ok()) foo.sor().value();
3593 }
3594 )cc");
3595
3596 // Non-const accessor.
3597 ExpectDiagnosticsFor(
3598 R"cc(
3599 #include "unchecked_statusor_access_test_defs.h"
3600
3601 struct Foo {
3602 STATUSOR_INT sor_;
3603
3604 const STATUSOR_INT& sor() { return sor_; }
3605 };
3606
3607 void target(Foo foo) {
3608 if (foo.sor().ok()) foo.sor().value(); // [[unsafe]]
3609 }
3610 )cc");
3611
3612 // Non-const rvalue accessor.
3613 ExpectDiagnosticsFor(
3614 R"cc(
3615 #include "unchecked_statusor_access_test_defs.h"
3616
3617 struct Foo {
3618 STATUSOR_INT sor_;
3619
3620 STATUSOR_INT&& sor() { return std::move(sor_); }
3621 };
3622
3623 void target(Foo foo) {
3624 if (foo.sor().ok()) foo.sor().value(); // [[unsafe]]
3625 }
3626 )cc");
3627
3628 // const pointer accessor.
3629 ExpectDiagnosticsFor(
3630 R"cc(
3631 #include "unchecked_statusor_access_test_defs.h"
3632
3633 struct Foo {
3634 STATUSOR_INT sor_;
3635
3636 const STATUSOR_INT* sor() const { return &sor_; }
3637 };
3638
3639 void target(Foo foo) {
3640 if (foo.sor()->ok()) foo.sor()->value();
3641 }
3642 )cc");
3643
3644 // const pointer operator.
3645 ExpectDiagnosticsFor(
3646 R"cc(
3647 #include "unchecked_statusor_access_test_defs.h"
3648
3649 struct Foo {
3650 STATUSOR_INT sor_;
3651
3652 const STATUSOR_INT* operator->() const { return &sor_; }
3653 };
3654
3655 void target(Foo foo) {
3656 if (foo->ok()) foo->value();
3657 }
3658 )cc");
3659
3660 // We copy the result of the accessor.
3661 ExpectDiagnosticsFor(R"cc(
3662 #include "unchecked_statusor_access_test_defs.h"
3663
3664 struct Foo {
3665 STATUSOR_INT sor_;
3666
3667 const STATUSOR_INT& sor() const { return sor_; }
3668 };
3669 void target() {
3670 Foo foo;
3671 if (!foo.sor().ok()) return;
3672 const auto sor = foo.sor();
3673 sor.value();
3674 }
3675 )cc");
3676 }
3677
3678 TEST_P(UncheckedStatusOrAccessModelTest, PointerLike) {
3679 ExpectDiagnosticsFor(R"cc(
3680 #include "unchecked_statusor_access_test_defs.h"
3681
3682 class Foo {
3683 public:
3684 std::pair<int, STATUSOR_VOIDPTR>& operator*() const;
3685 std::pair<int, STATUSOR_VOIDPTR>* operator->() const;
3686 bool operator!=(const Foo& other) const;
3687 };
3688
3689 void target() {
3690 Foo foo;
3691 if (foo->second.ok() && *foo->second != nullptr) {
3692 *foo->second;
3693 (*foo).second.value();
3694 }
3695 }
3696 )cc");
3697 ExpectDiagnosticsFor(R"cc(
3698 #include "unchecked_statusor_access_test_defs.h"
3699
3700 class Foo {
3701 public:
3702 std::pair<int, STATUSOR_INT>& operator*() const;
3703 std::pair<int, STATUSOR_INT>* operator->() const;
3704 };
3705 void target() {
3706 Foo foo;
3707 if (!foo->second.ok()) return;
3708 foo->second.value();
3709 (*foo).second.value();
3710 }
3711 )cc");
3712 ExpectDiagnosticsFor(R"cc(
3713 #include "unchecked_statusor_access_test_defs.h"
3714
3715 void target(std::pair<int, STATUSOR_VOIDPTR>* foo) {
3716 if (foo->second.ok() && *foo->second != nullptr) {
3717 *foo->second;
3718 (*foo).second.value();
3719 }
3720 }
3721 )cc");
3722 }
3723
3724 TEST_P(UncheckedStatusOrAccessModelTest, UniquePtr) {
3725 ExpectDiagnosticsFor(
3726 R"cc(
3727 #include "unchecked_statusor_access_test_defs.h"
3728
3729 void target() {
3730 auto sor_up = Make<std::unique_ptr<STATUSOR_INT>>();
3731 if (sor_up->ok()) sor_up->value();
3732 }
3733 )cc");
3734 }
3735
3736 TEST_P(UncheckedStatusOrAccessModelTest, UniquePtrReset) {
3737 ExpectDiagnosticsFor(
3738 R"cc(
3739 #include "unchecked_statusor_access_test_defs.h"
3740
3741 void target() {
3742 auto sor_up = Make<std::unique_ptr<STATUSOR_INT>>();
3743 if (sor_up->ok()) {
3744 sor_up.reset(Make<STATUSOR_INT*>());
3745 sor_up->value(); // [[unsafe]]
3746 }
3747 }
3748 )cc");
3749 }
3750
3751 TEST_P(UncheckedStatusOrAccessModelTest, NestedStatusOrInStatusOrStruct) {
3752 // Non-standard assignment with a nested StatusOr.
3753 ExpectDiagnosticsFor(
3754 R"cc(
3755 #include "unchecked_statusor_access_test_defs.h"
3756
3757 struct Inner {
3758 absl::StatusOr<std::string> sor;
3759 };
3760
3761 struct Outer {
3762 absl::StatusOr<Inner> inner;
3763 };
3764
3765 void target() {
3766 Outer foo = Make<Outer>();
3767 foo.inner->sor = "a"; // [[unsafe]]
3768 }
3769 )cc");
3770
3771 ExpectDiagnosticsFor(
3772 R"cc(
3773 #include "unchecked_statusor_access_test_defs.h"
3774
3775 struct Foo {
3776 absl::StatusOr<std::string> sor;
3777 };
3778
3779 void target(const absl::StatusOr<Foo>& foo) {
3780 if (foo.ok() && foo->sor.ok()) foo->sor.value();
3781 }
3782 )cc");
3783
3784 ExpectDiagnosticsFor(
3785 R"cc(
3786 #include "unchecked_statusor_access_test_defs.h"
3787
3788 struct Foo {
3789 absl::StatusOr<std::string> sor;
3790 };
3791
3792 void target(const absl::StatusOr<Foo>& foo) {
3793 if (foo.ok() && foo.value().sor.ok()) foo->sor.value();
3794 }
3795 )cc");
3796
3797 ExpectDiagnosticsFor(
3798 R"cc(
3799 #include "unchecked_statusor_access_test_defs.h"
3800
3801 struct Foo {
3802 absl::StatusOr<std::string> sor;
3803 };
3804
3805 void target(const absl::StatusOr<Foo>& foo) {
3806 if (foo.ok() && (*foo).sor.ok()) (*foo).sor.value();
3807 }
3808 )cc");
3809
3810 ExpectDiagnosticsFor(
3811 R"cc(
3812 #include "unchecked_statusor_access_test_defs.h"
3813
3814 struct Foo {
3815 absl::StatusOr<std::string> sor;
3816 };
3817
3818 void target(absl::StatusOr<Foo>& foo) {
3819 if (foo.ok() && foo->sor.ok()) *foo->sor;
3820 }
3821 )cc");
3822 // With assignment.
3823 ExpectDiagnosticsFor(
3824 R"cc(
3825 #include "unchecked_statusor_access_test_defs.h"
3826
3827 struct Foo {
3828 absl::StatusOr<std::string> sor;
3829 };
3830
3831 void target(absl::StatusOr<Foo>& foo) {
3832 if (foo.ok() && foo->sor.ok()) {
3833 foo->sor = Make<absl::StatusOr<std::string>>();
3834 foo->sor.value(); // [[unsafe]]
3835 }
3836 }
3837 )cc");
3838
3839 ExpectDiagnosticsFor(
3840 R"cc(
3841 #include "unchecked_statusor_access_test_defs.h"
3842
3843 struct Foo {
3844 absl::StatusOr<std::string> sor;
3845 };
3846
3847 void target(absl::StatusOr<Foo>& foo) {
3848 if (foo.ok() && foo->sor.ok()) {
3849 auto n = Make<absl::StatusOr<std::string>>();
3850 if (n.ok()) foo->sor = n;
3851 foo->sor.value();
3852 }
3853 }
3854 )cc");
3855
3856 ExpectDiagnosticsFor(
3857 R"cc(
3858 #include "unchecked_statusor_access_test_defs.h"
3859
3860 struct Foo {
3861 absl::StatusOr<std::string> sor;
3862 };
3863
3864 void target(absl::StatusOr<Foo>& foo) {
3865 if (foo.ok() && foo->sor.ok()) {
3866 auto n = Make<absl::StatusOr<std::string>>();
3867 if (n.ok()) foo->sor = std::move(n);
3868 foo->sor.value();
3869 }
3870 }
3871 )cc");
3872
3873 // More complicated conditionals.
3874 ExpectDiagnosticsFor(
3875 R"cc(
3876 #include "unchecked_statusor_access_test_defs.h"
3877
3878 struct Foo {
3879 absl::StatusOr<std::string> sor;
3880 };
3881
3882 void target(absl::StatusOr<Foo>& foo) {
3883 if (!foo.ok()) return;
3884 if (!foo->sor.ok())
3885 foo->sor.value(); // [[unsafe]]
3886 else
3887 foo->sor.value();
3888 }
3889 )cc");
3890
3891 ExpectDiagnosticsFor(
3892 R"cc(
3893 #include "unchecked_statusor_access_test_defs.h"
3894
3895 struct Foo {
3896 absl::StatusOr<std::string> sor;
3897 };
3898
3899 void target(absl::StatusOr<Foo>& foo, bool b) {
3900 if (!foo.ok()) return;
3901 if (b) {
3902 if (!foo->sor.ok()) return;
3903 foo->sor.value();
3904 } else {
3905 if (!foo->sor.ok()) return;
3906 foo->sor.value();
3907 }
3908 foo->sor.value();
3909 }
3910 )cc");
3911 }
3912
3913 TEST_P(UncheckedStatusOrAccessModelTest, StatusOrPtrReference) {
3914 ExpectDiagnosticsFor(R"cc(
3915 #include "unchecked_statusor_access_test_defs.h"
3916
3917 const STATUSOR_INT* foo();
3918
3919 void target() {
3920 const auto& sor = foo();
3921 if (sor->ok()) sor->value();
3922 }
3923 )cc");
3924
3925 ExpectDiagnosticsFor(R"cc(
3926 #include "unchecked_statusor_access_test_defs.h"
3927
3928 using StatusOrPtr = const STATUSOR_INT*;
3929 StatusOrPtr foo();
3930
3931 void target() {
3932 const auto& sor = foo();
3933 if (sor->ok()) sor->value();
3934 }
3935 )cc");
3936 }
3937
3938 TEST_P(UncheckedStatusOrAccessModelTest, StatusPtrReference) {
3939 ExpectDiagnosticsFor(R"cc(
3940 #include "unchecked_statusor_access_test_defs.h"
3941
3942 const STATUS* foo();
3943
3944 void target(STATUSOR_INT sor) {
3945 const auto& s = foo();
3946 if (s->ok() && *s == sor.status()) sor.value();
3947 }
3948 )cc");
3949
3950 ExpectDiagnosticsFor(R"cc(
3951 #include "unchecked_statusor_access_test_defs.h"
3952
3953 using StatusPtr = const STATUS*;
3954 StatusPtr foo();
3955
3956 void target(STATUSOR_INT sor) {
3957 const auto& s = foo();
3958 if (s->ok() && *s == sor.status()) sor.value();
3959 }
3960 )cc");
3961 }
3962
3963 } // namespace
3964
3965 std::string
3966 GetAliasMacros(UncheckedStatusOrAccessModelTestAliasKind AliasKind) {
3967 switch (AliasKind) {
3968 case UncheckedStatusOrAccessModelTestAliasKind::kUnaliased:
3969 return R"cc(
3970 #define STATUSOR_INT ::absl::StatusOr<int>
3971 #define STATUSOR_BOOL ::absl::StatusOr<bool>
3972 #define STATUSOR_VOIDPTR ::absl::StatusOr<void*>
3973 #define STATUS ::absl::Status
3974 )cc";
3975 case UncheckedStatusOrAccessModelTestAliasKind::kPartiallyAliased:
3976 return R"cc(
3977 template <typename T>
3978 using StatusOrAlias = ::absl::StatusOr<T>;
3979 #define STATUSOR_INT StatusOrAlias<int>
3980 #define STATUSOR_BOOL StatusOrAlias<bool>
3981 #define STATUSOR_VOIDPTR StatusOrAlias<void*>
3982 #define STATUS ::absl::Status
3983 )cc";
3984 case UncheckedStatusOrAccessModelTestAliasKind::kFullyAliased:
3985 return R"cc(
3986 using StatusOrIntAlias = ::absl::StatusOr<int>;
3987 #define STATUSOR_INT StatusOrIntAlias
3988 using StatusOrBoolAlias = ::absl::StatusOr<bool>;
3989 #define STATUSOR_BOOL StatusOrBoolAlias
3990 using StatusOrVoidPtrAlias = ::absl::StatusOr<void*>;
3991 #define STATUSOR_VOIDPTR StatusOrVoidPtrAlias
3992 using StatusAlias = ::absl::Status;
3993 #define STATUS StatusAlias
3994 )cc";
3995 }
3996 llvm_unreachable("Unknown alias kind.");
3997 }
3998
3999 std::vector<std::pair<std::string, std::string>>
4000 GetHeaders(UncheckedStatusOrAccessModelTestAliasKind AliasKind) {
4001 auto Headers = test::getMockHeaders();
4002
4003 Headers.emplace_back("unchecked_statusor_access_test_defs.h",
4004 R"cc(
4005 #include "cstddef.h"
4006 #include "statusor_defs.h"
4007 #include "std_optional.h"
4008 #include "std_vector.h"
4009 #include "std_pair.h"
4010 #include "absl_log.h"
4011 #include "testing_defs.h"
4012 #include "std_unique_ptr.h"
4013
4014 template <typename T>
4015 T Make();
4016
4017 class Fatal {
4018 public:
4019 ~Fatal() __attribute__((noreturn));
4020 int value();
4021 };
4022 )cc" +
4023 GetAliasMacros(AliasKind));
4024 return Headers;
4025 }
4026 } // namespace clang::dataflow::statusor_model