3252 lines · cpp
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-exception6//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 19namespace clang::dataflow::statusor_model {20namespace {21 22TEST_P(UncheckedStatusOrAccessModelTest, NoStatusOrMention) {23 ExpectDiagnosticsFor(R"cc(24 void target() { "nop"; }25 )cc");26}27 28TEST_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 39TEST_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 49TEST_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 newline56 value(); // [[unsafe]]57 }58 )cc");59}60 61TEST_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 72TEST_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 83TEST_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 94TEST_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 105TEST_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 117TEST_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 128TEST_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 143TEST_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 158TEST_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 168TEST_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 179TEST_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 218TEST_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 else227 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 else239 sor.value(); // [[unsafe]]240 }241 )cc");242}243 244TEST_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 else254 sor.value();255 }256 }257 )cc");258}259 260TEST_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 else268 sor.value(); // [[unsafe]]269 }270 )cc");271}272 273TEST_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 else282 sor.value();283 284 sor.value(); // [[unsafe]]285 }286 )cc");287}288 289TEST_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 else297 sor.value(); // [[unsafe]]298 }299 )cc");300}301 302TEST_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 else310 sor.value();311 }312 )cc");313}314 315TEST_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 333TEST_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 352TEST_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 371TEST_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 389TEST_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 408TEST_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 427TEST_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 446TEST_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 464TEST_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 482TEST_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 501TEST_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 520TEST_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 539TEST_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 557TEST_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 576TEST_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 595TEST_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 613TEST_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 646TEST_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 673TEST_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 702TEST_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)0727 : (void)1;728 do {729 sor1.value(); // [[unsafe]]730 sor2.value(); // [[unsafe]]731 } while (true);732 }733 )cc");734}735 736TEST_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 774TEST_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 789TEST_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 806TEST_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 823TEST_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 840TEST_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 857TEST_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 874TEST_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 891TEST_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 908TEST_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 925TEST_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 942TEST_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 959TEST_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 976TEST_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 993TEST_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 1010TEST_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 1027TEST_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 1042TEST_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 1065TEST_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 1086TEST_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 1117TEST_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 1132TEST_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 1204TEST_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 1273TEST_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 1341TEST_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 1466TEST_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 1507TEST_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 1550TEST_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 1587TEST_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 1605TEST_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 1625TEST_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 1690TEST_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 1709TEST_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 1728TEST_P(UncheckedStatusOrAccessModelTest, CheckNeMacro) {1729 ExpectDiagnosticsFor(R"cc(1730#include "unchecked_statusor_access_test_defs.h"1731 1732 void target(STATUSOR_INT sor) {1733 CHECK_NE(sor.status(), absl::OkStatus());1734 sor.value(); // [[unsafe]]1735 }1736 )cc");1737}1738 1739TEST_P(UncheckedStatusOrAccessModelTest, QcheckNeMacro) {1740 ExpectDiagnosticsFor(R"cc(1741#include "unchecked_statusor_access_test_defs.h"1742 1743 void target(STATUSOR_INT sor) {1744 QCHECK_NE(sor.status(), absl::OkStatus());1745 sor.value(); // [[unsafe]]1746 }1747 )cc");1748}1749 1750TEST_P(UncheckedStatusOrAccessModelTest, GlobalVars) {1751 // The following examples are not sound as there could be opaque calls between1752 // the ok() and the value() calls that change the StatusOr value.1753 ExpectDiagnosticsFor(R"cc(1754#include "unchecked_statusor_access_test_defs.h"1755 1756 static STATUSOR_INT sor;1757 1758 void target() {1759 if (sor.ok())1760 sor.value();1761 else1762 sor.value(); // [[unsafe]]1763 }1764 )cc");1765 ExpectDiagnosticsFor(R"cc(1766#include "unchecked_statusor_access_test_defs.h"1767 1768 void target() {1769 static STATUSOR_INT sor;1770 if (sor.ok())1771 sor.value();1772 else1773 sor.value(); // [[unsafe]]1774 }1775 )cc");1776 ExpectDiagnosticsFor(R"cc(1777#include "unchecked_statusor_access_test_defs.h"1778 1779 struct Foo {1780 static STATUSOR_INT sor;1781 };1782 1783 void target(Foo foo) {1784 if (foo.sor.ok())1785 foo.sor.value();1786 else1787 foo.sor.value(); // [[unsafe]]1788 }1789 )cc");1790 ExpectDiagnosticsFor(R"cc(1791#include "unchecked_statusor_access_test_defs.h"1792 1793 struct Foo {1794 static STATUSOR_INT sor;1795 };1796 1797 void target() {1798 if (Foo::sor.ok())1799 Foo::sor.value();1800 else1801 Foo::sor.value(); // [[unsafe]]1802 }1803 )cc");1804 ExpectDiagnosticsFor(R"cc(1805#include "unchecked_statusor_access_test_defs.h"1806 1807 struct Foo {1808 static STATUSOR_INT sor;1809 1810 static void target() {1811 if (sor.ok())1812 sor.value();1813 else1814 sor.value(); // [[unsafe]]1815 }1816 };1817 )cc");1818 ExpectDiagnosticsFor(R"cc(1819#include "unchecked_statusor_access_test_defs.h"1820 1821 struct Foo {1822 static STATUSOR_INT sor;1823 1824 void target() {1825 if (sor.ok())1826 sor.value();1827 else1828 sor.value(); // [[unsafe]]1829 }1830 };1831 )cc");1832 ExpectDiagnosticsFor(R"cc(1833#include "unchecked_statusor_access_test_defs.h"1834 1835 struct S {1836 static const int x = -1;1837 };1838 1839 int target(S s) { return s.x; }1840 )cc");1841}1842 1843TEST_P(UncheckedStatusOrAccessModelTest, ReferenceReceivers) {1844 // The following examples are not sound as there could be opaque calls between1845 // the ok() and the value() calls that change the StatusOr value. However,1846 // this is the behavior that users expect so it is here to stay.1847 ExpectDiagnosticsFor(R"cc(1848#include "unchecked_statusor_access_test_defs.h"1849 1850 void target(STATUSOR_INT& sor) {1851 if (sor.ok())1852 sor.value();1853 else1854 sor.value(); // [[unsafe]]1855 }1856 )cc");1857 ExpectDiagnosticsFor(R"cc(1858#include "unchecked_statusor_access_test_defs.h"1859 1860 struct Foo {1861 STATUSOR_INT& sor;1862 };1863 1864 void target(Foo foo) {1865 if (foo.sor.ok())1866 foo.sor.value();1867 else1868 foo.sor.value(); // [[unsafe]]1869 }1870 )cc");1871 ExpectDiagnosticsFor(R"cc(1872#include "unchecked_statusor_access_test_defs.h"1873 1874 struct Bar {1875 STATUSOR_INT sor;1876 };1877 1878 struct Foo {1879 Bar& bar;1880 };1881 1882 void target(Foo foo) {1883 if (foo.bar.sor.ok())1884 foo.bar.sor.value();1885 else1886 foo.bar.sor.value(); // [[unsafe]]1887 }1888 )cc");1889 ExpectDiagnosticsFor(R"cc(1890#include "unchecked_statusor_access_test_defs.h"1891 1892 struct Foo {1893 STATUSOR_INT& sor;1894 };1895 1896 void target(Foo& foo) {1897 if (foo.sor.ok())1898 foo.sor.value();1899 else1900 foo.sor.value(); // [[unsafe]]1901 }1902 )cc");1903}1904 1905TEST_P(UncheckedStatusOrAccessModelTest, Lambdas) {1906 ExpectDiagnosticsForLambda(R"cc(1907#include "unchecked_statusor_access_test_defs.h"1908 1909 void target() {1910 [](STATUSOR_INT sor) {1911 if (sor.ok())1912 sor.value();1913 else1914 sor.value(); // [[unsafe]]1915 }(Make<STATUSOR_INT>());1916 }1917 )cc");1918 ExpectDiagnosticsForLambda(R"cc(1919#include "unchecked_statusor_access_test_defs.h"1920 1921 void target(STATUSOR_INT sor) {1922 [sor]() {1923 if (sor.ok())1924 sor.value();1925 else1926 sor.value(); // [[unsafe]]1927 }();1928 }1929 )cc");1930 ExpectDiagnosticsForLambda(R"cc(1931#include "unchecked_statusor_access_test_defs.h"1932 1933 void target(STATUSOR_INT sor) {1934 [&sor]() {1935 if (sor.ok())1936 sor.value();1937 else1938 sor.value(); // [[unsafe]]1939 }();1940 }1941 )cc");1942 ExpectDiagnosticsForLambda(R"cc(1943#include "unchecked_statusor_access_test_defs.h"1944 1945 void target(STATUSOR_INT sor) {1946 [sor2 = sor]() {1947 if (sor2.ok())1948 sor2.value();1949 else1950 sor2.value(); // [[unsafe]]1951 }();1952 }1953 )cc");1954 ExpectDiagnosticsForLambda(R"cc(1955#include "unchecked_statusor_access_test_defs.h"1956 1957 void target(STATUSOR_INT sor) {1958 [&]() {1959 if (sor.ok())1960 sor.value();1961 else1962 sor.value(); // [[unsafe]]1963 }();1964 }1965 )cc");1966 ExpectDiagnosticsForLambda(R"cc(1967#include "unchecked_statusor_access_test_defs.h"1968 1969 void target(STATUSOR_INT sor) {1970 [=]() {1971 if (sor.ok())1972 sor.value();1973 else1974 sor.value(); // [[unsafe]]1975 }();1976 }1977 )cc");1978 ExpectDiagnosticsForLambda(R"cc(1979#include "unchecked_statusor_access_test_defs.h"1980 1981 struct Foo {1982 STATUSOR_INT sor;1983 1984 void target() {1985 [this]() {1986 if (sor.ok())1987 sor.value();1988 else1989 sor.value(); // [[unsafe]]1990 }();1991 }1992 };1993 )cc");1994}1995 1996TEST_P(UncheckedStatusOrAccessModelTest, GoodLambda) {1997 ExpectDiagnosticsFor(R"cc(1998#include "unchecked_statusor_access_test_defs.h"1999 2000 int target() {2001 STATUSOR_INT sor = Make<STATUSOR_INT>();2002 if (sor.ok()) return [&s = sor.value()] { return s; }();2003 return 0;2004 }2005 )cc");2006}2007 2008TEST_P(UncheckedStatusOrAccessModelTest, Status) {2009 ExpectDiagnosticsFor(R"cc(2010#include "unchecked_statusor_access_test_defs.h"2011 2012 void foo();2013 2014 void target(STATUS s) {2015 if (s.ok()) foo();2016 }2017 )cc");2018 ExpectDiagnosticsFor(R"cc(2019#include "unchecked_statusor_access_test_defs.h"2020 2021 void foo();2022 2023 void target() {2024 STATUS s = Make<STATUSOR_INT>().status();2025 if (s.ok()) foo();2026 }2027 )cc");2028}2029 2030TEST_P(UncheckedStatusOrAccessModelTest, ExpectThatMacro) {2031 ExpectDiagnosticsFor(R"cc(2032#include "unchecked_statusor_access_test_defs.h"2033 2034 void target(STATUSOR_INT sor) {2035 EXPECT_THAT(sor, testing::status::IsOk());2036 2037 sor.value(); // [[unsafe]]2038 }2039 )cc");2040 ExpectDiagnosticsFor(R"cc(2041#include "unchecked_statusor_access_test_defs.h"2042 2043 void target(STATUSOR_INT sor) {2044 EXPECT_THAT(sor.status(), testing::status::IsOk());2045 2046 sor.value(); // [[unsafe]]2047 }2048 )cc");2049 ExpectDiagnosticsFor(R"cc(2050#include "unchecked_statusor_access_test_defs.h"2051 2052 void target() {2053 STATUSOR_INT sor = Make<STATUSOR_INT>();2054 EXPECT_THAT(sor, testing::status::IsOk());2055 2056 sor.value(); // [[unsafe]]2057 }2058 )cc");2059}2060 2061TEST_P(UncheckedStatusOrAccessModelTest, ExpectOkMacro) {2062 ExpectDiagnosticsFor(R"cc(2063#include "unchecked_statusor_access_test_defs.h"2064 2065 void target(STATUSOR_INT sor) {2066 EXPECT_OK(sor);2067 2068 sor.value(); // [[unsafe]]2069 }2070 )cc");2071 ExpectDiagnosticsFor(R"cc(2072#include "unchecked_statusor_access_test_defs.h"2073 2074 void target(STATUSOR_INT sor) {2075 EXPECT_OK(sor.status());2076 2077 sor.value(); // [[unsafe]]2078 }2079 )cc");2080 ExpectDiagnosticsFor(R"cc(2081#include "unchecked_statusor_access_test_defs.h"2082 2083 void target() {2084 STATUSOR_INT sor = Make<STATUSOR_INT>();2085 EXPECT_OK(sor);2086 2087 sor.value(); // [[unsafe]]2088 }2089 )cc");2090}2091 2092TEST_P(UncheckedStatusOrAccessModelTest, BreadthFirstBlockTraversalLoop) {2093 // Evaluating the CFG blocks of the code below in breadth-first order results2094 // in an infinite loop. Each iteration of the while loop below results in a2095 // new value being assigned to the storage location of sor1. However,2096 // following a bread-first order of evaluation, downstream blocks will join2097 // environments of different generations of predecessor blocks having distinct2098 // values assigned to the sotrage location of sor1, resulting in not assigning2099 // a value to the storage location of sor1 in successors. As iterations of the2100 // analysis go, the state of the environment flips between having a value2101 // assigned to the storage location of sor1 and not having a value assigned to2102 // it. Since the evaluation of the copy constructor expression in bar(sor1)2103 // depends on a value being assigned to sor1, the state of the environment2104 // also flips between having a storage location assigned to the bar(sor1)2105 // expression and not having a storage location assigned to it. This leads to2106 // an infinite loop as the environment can't stabilize.2107 ExpectDiagnosticsFor(R"cc(2108#include "unchecked_statusor_access_test_defs.h"2109 2110 void foo(int, int);2111 STATUSOR_INT bar(STATUSOR_INT);2112 void baz(int);2113 2114 void target() {2115 while (true) {2116 STATUSOR_INT sor1 = Make<STATUSOR_INT>();2117 if (sor1.ok()) {2118 STATUSOR_INT sor2 = Make<STATUSOR_INT>();2119 if (sor2.ok()) foo(sor1.value(), sor2.value());2120 }2121 2122 STATUSOR_INT sor3 = bar(sor1);2123 for (int i = 0; i < 5; i++) sor3 = bar(sor1);2124 2125 baz(sor3.value()); // [[unsafe]]2126 }2127 }2128 )cc");2129}2130 2131TEST_P(UncheckedStatusOrAccessModelTest, ReturnValue) {2132 ExpectDiagnosticsFor(R"cc(2133#include "unchecked_statusor_access_test_defs.h"2134 2135 void target() {2136 STATUSOR_INT sor = Make<STATUSOR_INT>();2137 if (sor.ok())2138 sor.value();2139 else2140 sor.value(); // [[unsafe]]2141 }2142 )cc");2143}2144 2145TEST_P(UncheckedStatusOrAccessModelTest, Goto) {2146 ExpectDiagnosticsFor(R"cc(2147#include "unchecked_statusor_access_test_defs.h"2148 2149 void target(STATUSOR_INT sor) {2150 label:2151 if (sor.ok())2152 sor.value();2153 else2154 sor.value(); // [[unsafe]]2155 goto label;2156 }2157 )cc");2158 ExpectDiagnosticsFor(R"cc(2159#include "unchecked_statusor_access_test_defs.h"2160 2161 void target(STATUSOR_INT sor) {2162 label:2163 if (!sor.ok()) goto label;2164 sor.value();2165 }2166 )cc");2167 ExpectDiagnosticsFor(R"cc(2168#include "unchecked_statusor_access_test_defs.h"2169 2170 void target(STATUSOR_INT sor) {2171 if (!sor.ok()) return;2172 goto label;2173 label:2174 sor.value();2175 }2176 )cc");2177}2178 2179TEST_P(UncheckedStatusOrAccessModelTest, JoinDistinctValues) {2180 ExpectDiagnosticsFor(R"cc(2181#include "unchecked_statusor_access_test_defs.h"2182 2183 void target(bool b) {2184 STATUSOR_INT sor;2185 if (b)2186 sor = Make<STATUSOR_INT>();2187 else2188 sor = Make<STATUSOR_INT>();2189 2190 if (sor.ok())2191 sor.value();2192 else2193 sor.value(); // [[unsafe]]2194 }2195 )cc");2196 ExpectDiagnosticsFor(R"cc(2197#include "unchecked_statusor_access_test_defs.h"2198 2199 void target(bool b) {2200 STATUSOR_INT sor;2201 if (b) {2202 sor = Make<STATUSOR_INT>();2203 if (!sor.ok()) return;2204 } else {2205 sor = Make<STATUSOR_INT>();2206 if (!sor.ok()) return;2207 }2208 sor.value();2209 }2210 )cc");2211 ExpectDiagnosticsFor(R"cc(2212#include "unchecked_statusor_access_test_defs.h"2213 2214 void target(bool b) {2215 STATUSOR_INT sor;2216 if (b) {2217 sor = Make<STATUSOR_INT>();2218 if (!sor.ok()) return;2219 } else {2220 sor = Make<STATUSOR_INT>();2221 }2222 sor.value(); // [[unsafe]]2223 }2224 )cc");2225}2226 2227TEST_P(UncheckedStatusOrAccessModelTest, VarDeclInitExprFromPairAccess) {2228 ExpectDiagnosticsFor(R"cc(2229#include "unchecked_statusor_access_test_defs.h"2230 2231 void target() {2232 auto sor = Make<std::pair<int, STATUSOR_INT>>().second;2233 if (sor.ok())2234 sor.value();2235 else2236 sor.value(); // [[unsafe]]2237 }2238 )cc");2239 ExpectDiagnosticsFor(R"cc(2240#include "unchecked_statusor_access_test_defs.h"2241 2242 void target() {2243 const auto& sor = Make<std::pair<int, STATUSOR_INT>>().second;2244 if (sor.ok())2245 sor.value();2246 else2247 sor.value(); // [[unsafe]]2248 }2249 )cc");2250}2251 2252TEST_P(UncheckedStatusOrAccessModelTest, LValueToRValueCastOfChangingValue) {2253 ExpectDiagnosticsFor(R"cc(2254#include "unchecked_statusor_access_test_defs.h"2255 2256 bool foo();2257 2258 void target(bool b1) {2259 STATUSOR_INT sor;2260 if (b1)2261 sor = Make<STATUSOR_INT>();2262 else2263 sor = Make<STATUSOR_INT>();2264 2265 do {2266 const auto& b2 = foo();2267 if (b2) break;2268 2269 sor.value(); // [[unsafe]]2270 } while (true);2271 }2272 )cc");2273}2274 2275TEST_P(UncheckedStatusOrAccessModelTest, ConstructorInitializer) {2276 ExpectDiagnosticsFor(R"cc(2277#include "unchecked_statusor_access_test_defs.h"2278 2279 class target {2280 target() : foo_(Make<STATUSOR_INT>().value()) { // [[unsafe]]2281 }2282 int foo_;2283 };2284 )cc");2285}2286 2287TEST_P(UncheckedStatusOrAccessModelTest, AssignStatusToBoolVar) {2288 ExpectDiagnosticsFor(R"cc(2289#include "unchecked_statusor_access_test_defs.h"2290 2291 void target(STATUSOR_INT sor) {2292 bool ok = sor.ok();2293 if (ok)2294 sor.value();2295 else2296 sor.value(); // [[unsafe]]2297 }2298 )cc");2299 ExpectDiagnosticsFor(R"cc(2300#include "unchecked_statusor_access_test_defs.h"2301 2302 void target(STATUSOR_INT sor) {2303 bool not_ok = !sor.ok();2304 if (not_ok)2305 sor.value(); // [[unsafe]]2306 else2307 sor.value();2308 }2309 )cc");2310}2311 2312TEST_P(UncheckedStatusOrAccessModelTest, StructuredBindings) {2313 // Binding to a pair (which is actually a struct in the mock header).2314 ExpectDiagnosticsFor(R"cc(2315#include "unchecked_statusor_access_test_defs.h"2316 2317 void target() {2318 const auto [sor, x] = Make<std::pair<STATUSOR_INT, int>>();2319 if (sor.ok()) sor.value();2320 }2321 )cc");2322 2323 // Unsafe case.2324 ExpectDiagnosticsFor(R"cc(2325#include "unchecked_statusor_access_test_defs.h"2326 2327 void target() {2328 const auto [sor, x] = Make<std::pair<STATUSOR_INT, int>>();2329 sor.value(); // [[unsafe]]2330 }2331 )cc");2332 2333 // As a reference.2334 ExpectDiagnosticsFor(R"cc(2335#include "unchecked_statusor_access_test_defs.h"2336 2337 void target() {2338 const auto& [sor, x] = Make<std::pair<STATUSOR_INT, int>>();2339 if (sor.ok()) sor.value();2340 }2341 )cc");2342 2343 // Binding to a ref in a struct.2344 ExpectDiagnosticsFor(R"cc(2345#include "unchecked_statusor_access_test_defs.h"2346 2347 struct S {2348 STATUSOR_INT& sor;2349 int i;2350 };2351 2352 void target() {2353 const auto& [sor, i] = Make<S>();2354 if (sor.ok()) sor.value();2355 }2356 )cc");2357 2358 // In a loop.2359 ExpectDiagnosticsFor(R"cc(2360#include "unchecked_statusor_access_test_defs.h"2361 2362 void target() {2363 auto vals = Make<std::vector<std::pair<int, STATUSOR_INT>>>();2364 for (const auto& [x, sor] : vals)2365 if (sor.ok()) sor.value();2366 }2367 )cc");2368 2369 // Similar to the above, but InitExpr already has the storage initialized,2370 // and bindings refer to them.2371 ExpectDiagnosticsFor(R"cc(2372#include "unchecked_statusor_access_test_defs.h"2373 2374 void target() {2375 auto vals = Make<std::vector<std::pair<int, STATUSOR_INT>>>();2376 for (const auto& p : vals) {2377 const auto& [i, sor] = p;2378 if (sor.ok()) sor.value();2379 }2380 }2381 )cc");2382}2383 2384TEST_P(UncheckedStatusOrAccessModelTest, AssignCompositeLogicExprToVar) {2385 ExpectDiagnosticsFor(2386 R"cc(2387#include "unchecked_statusor_access_test_defs.h"2388 2389 void target(STATUSOR_INT sor, bool b) {2390 bool c = sor.ok() && b;2391 if (c) sor.value();2392 }2393 )cc");2394 2395 ExpectDiagnosticsFor(2396 R"cc(2397#include "unchecked_statusor_access_test_defs.h"2398 2399 void target(STATUSOR_INT sor, bool b) {2400 bool c = !(!sor.ok() || !b);2401 if (c) sor.value();2402 }2403 )cc");2404}2405 2406TEST_P(UncheckedStatusOrAccessModelTest, Subclass) {2407 ExpectDiagnosticsFor(2408 R"cc(2409#include "unchecked_statusor_access_test_defs.h"2410 2411 class Foo : public STATUSOR_INT {};2412 2413 void target(Foo opt) {2414 opt.value(); // [[unsafe]]2415 }2416 )cc");2417}2418 2419TEST_P(UncheckedStatusOrAccessModelTest, SubclassStatus) {2420 ExpectDiagnosticsFor(2421 R"cc(2422#include "unchecked_statusor_access_test_defs.h"2423 2424 class Foo : public STATUS {};2425 2426 void target(Foo opt) { opt.ok(); }2427 )cc");2428}2429 2430TEST_P(UncheckedStatusOrAccessModelTest, SubclassOk) {2431 ExpectDiagnosticsFor(2432 R"cc(2433#include "unchecked_statusor_access_test_defs.h"2434 2435 class Foo : public STATUSOR_INT {};2436 2437 void target(Foo opt) {2438 if (opt.ok()) opt.value();2439 }2440 )cc");2441}2442 2443TEST_P(UncheckedStatusOrAccessModelTest, SubclassOperator) {2444 ExpectDiagnosticsFor(2445 R"cc(2446#include "unchecked_statusor_access_test_defs.h"2447 2448 class Foo : public STATUSOR_INT {};2449 2450 void target(Foo opt) {2451 *opt; // [[unsafe]]2452 }2453 )cc");2454}2455 2456TEST_P(UncheckedStatusOrAccessModelTest, UnwrapValueWithStatusCheck) {2457 ExpectDiagnosticsFor(R"cc(2458#include "unchecked_statusor_access_test_defs.h"2459 2460 void target(STATUSOR_INT sor) {2461 if (sor.status().ok())2462 sor.value();2463 else2464 sor.value(); // [[unsafe]]2465 }2466 )cc");2467}2468 2469TEST_P(UncheckedStatusOrAccessModelTest, UnwrapValueWithStatusRefCheck) {2470 ExpectDiagnosticsFor(R"cc(2471#include "unchecked_statusor_access_test_defs.h"2472 2473 void target(STATUSOR_INT sor) {2474 const STATUS& s = sor.status();2475 if (s.ok())2476 sor.value();2477 else2478 sor.value(); // [[unsafe]]2479 }2480 )cc");2481}2482 2483TEST_P(UncheckedStatusOrAccessModelTest, UnwrapValueWithStatusPtrCheck) {2484 ExpectDiagnosticsFor(R"cc(2485#include "unchecked_statusor_access_test_defs.h"2486 2487 void target(STATUSOR_INT sor) {2488 const STATUS* s = &sor.status();2489 if (s->ok())2490 sor.value();2491 else2492 sor.value(); // [[unsafe]]2493 }2494 )cc");2495}2496 2497TEST_P(UncheckedStatusOrAccessModelTest, UnwrapValueWithMovedStatus) {2498 ExpectDiagnosticsFor(R"cc(2499#include "unchecked_statusor_access_test_defs.h"2500 2501 void target(STATUSOR_INT sor) {2502 if (std::move(sor.status()).ok())2503 sor.value();2504 else2505 sor.value(); // [[unsafe]]2506 }2507 )cc");2508}2509 2510TEST_P(UncheckedStatusOrAccessModelTest, MembersUsedInsideStatus) {2511 ExpectDiagnosticsFor(R"cc(2512 namespace absl {2513 2514 class Status {2515 public:2516 bool ok() const;2517 2518 void target() const { ok(); }2519 };2520 2521 } // namespace absl2522 )cc");2523}2524 2525TEST_P(UncheckedStatusOrAccessModelTest, StatusUpdate) {2526 ExpectDiagnosticsFor(R"cc(2527#include "unchecked_statusor_access_test_defs.h"2528 2529 void target(STATUSOR_INT sor) {2530 STATUS s;2531 s.Update(sor.status());2532 if (s.ok())2533 sor.value();2534 else2535 sor.value(); // [[unsafe]]2536 }2537 )cc");2538 2539 ExpectDiagnosticsFor(R"cc(2540#include "unchecked_statusor_access_test_defs.h"2541 2542 void target(STATUSOR_INT sor1, STATUSOR_INT sor2) {2543 STATUS s;2544 s.Update(sor1.status());2545 s.Update(sor2.status());2546 if (s.ok()) {2547 sor1.value();2548 sor2.value();2549 }2550 }2551 )cc");2552 2553 ExpectDiagnosticsFor(R"cc(2554#include "unchecked_statusor_access_test_defs.h"2555 2556 void target(STATUSOR_INT sor1, STATUSOR_INT sor2) {2557 STATUS s;2558 s.Update(sor1.status());2559 CHECK(s.ok());2560 s.Update(sor2.status());2561 sor1.value();2562 sor2.value(); // [[unsafe]]2563 }2564 )cc");2565 2566 ExpectDiagnosticsFor(R"cc(2567#include "unchecked_statusor_access_test_defs.h"2568 2569 void target(STATUSOR_INT sor1, STATUSOR_INT sor2) {2570 STATUS s;2571 s.Update(sor1.status());2572 CHECK(s.ok());2573 sor1.value();2574 sor2.value(); // [[unsafe]]2575 }2576 )cc");2577 2578 ExpectDiagnosticsFor(R"cc(2579#include "unchecked_statusor_access_test_defs.h"2580 2581 void target(STATUSOR_INT sor1, STATUSOR_INT sor2) {2582 STATUS s;2583 STATUS sor1_status = sor1.status();2584 s.Update(std::move(sor1_status));2585 CHECK(s.ok());2586 sor1.value();2587 sor2.value(); // [[unsafe]]2588 }2589 )cc");2590 2591 ExpectDiagnosticsFor(R"cc(2592#include "unchecked_statusor_access_test_defs.h"2593 2594 void target(STATUSOR_INT sor1, STATUSOR_INT sor2) {2595 STATUS s;2596 STATUS sor1_status = sor1.status();2597 sor1_status.Update(sor2.status());2598 s.Update(std::move(sor1_status));2599 CHECK(s.ok());2600 sor1.value();2601 sor2.value();2602 }2603 )cc");2604 ExpectDiagnosticsFor(R"cc(2605#include "unchecked_statusor_access_test_defs.h"2606 2607 const STATUS& OptStatus();2608 2609 void target(STATUSOR_INT sor) {2610 auto s = sor.status();2611 s.Update(OptStatus());2612 if (s.ok()) sor.value();2613 }2614 )cc");2615}2616 2617TEST_P(UncheckedStatusOrAccessModelTest, EqualityCheck) {2618 ExpectDiagnosticsFor(2619 R"cc(2620#include "unchecked_statusor_access_test_defs.h"2621 2622 void target(STATUSOR_INT x, STATUSOR_INT y) {2623 if (x.ok()) {2624 if (x == y)2625 y.value();2626 else2627 y.value(); // [[unsafe]]2628 }2629 }2630 )cc");2631 ExpectDiagnosticsFor(2632 R"cc(2633#include "unchecked_statusor_access_test_defs.h"2634 2635 void target(STATUSOR_INT x, STATUSOR_INT y) {2636 if (x.ok()) {2637 if (y == x)2638 y.value();2639 else2640 y.value(); // [[unsafe]]2641 }2642 }2643 )cc");2644 ExpectDiagnosticsFor(R"cc(2645#include "unchecked_statusor_access_test_defs.h"2646 2647 void target(STATUSOR_INT x, STATUSOR_INT y) {2648 if (x.ok()) {2649 if (x != y)2650 y.value(); // [[unsafe]]2651 else2652 y.value();2653 }2654 }2655 )cc");2656 ExpectDiagnosticsFor(R"cc(2657#include "unchecked_statusor_access_test_defs.h"2658 2659 void target(STATUSOR_INT x, STATUSOR_INT y) {2660 if (x.ok()) {2661 if (y != x)2662 y.value(); // [[unsafe]]2663 else2664 y.value();2665 }2666 }2667 )cc");2668 ExpectDiagnosticsFor(R"cc(2669#include "unchecked_statusor_access_test_defs.h"2670 2671 void target(STATUSOR_INT x, STATUSOR_INT y) {2672 if (x.ok()) {2673 if (!(x == y))2674 y.value(); // [[unsafe]]2675 else2676 y.value();2677 }2678 }2679 )cc");2680 ExpectDiagnosticsFor(2681 R"cc(2682#include "unchecked_statusor_access_test_defs.h"2683 2684 void target(STATUSOR_INT x, STATUSOR_INT y) {2685 if (x.ok()) {2686 if (!(x != y))2687 y.value();2688 else2689 y.value(); // [[unsafe]]2690 }2691 }2692 )cc");2693 ExpectDiagnosticsFor(R"cc(2694#include "unchecked_statusor_access_test_defs.h"2695 2696 void target(STATUSOR_INT x, STATUSOR_INT y) {2697 if (x == y)2698 if (x.ok()) y.value();2699 }2700 )cc");2701 ExpectDiagnosticsFor(2702 R"cc(2703#include "unchecked_statusor_access_test_defs.h"2704 2705 void target(STATUSOR_INT x, STATUSOR_INT y) {2706 if (x.ok()) {2707 if (x.status() == y.status())2708 y.value();2709 else2710 y.value(); // [[unsafe]]2711 }2712 }2713 )cc");2714 ExpectDiagnosticsFor(R"cc(2715#include "unchecked_statusor_access_test_defs.h"2716 2717 void target(STATUSOR_INT x, STATUSOR_INT y) {2718 if (x.ok()) {2719 if (x.status() != y.status())2720 y.value(); // [[unsafe]]2721 else2722 y.value();2723 }2724 }2725 )cc");2726 ExpectDiagnosticsFor(R"cc(2727#include "unchecked_statusor_access_test_defs.h"2728 2729 void target(STATUSOR_INT sor) {2730 if (sor.status() == absl::OkStatus())2731 sor.value();2732 else2733 sor.value(); // [[unsafe]]2734 }2735 )cc");2736 ExpectDiagnosticsFor(R"cc(2737#include "unchecked_statusor_access_test_defs.h"2738 2739 void target(STATUSOR_INT sor) {2740 if (sor.status() != absl::OkStatus())2741 sor.value(); // [[unsafe]]2742 else2743 sor.value();2744 }2745 )cc");2746 ExpectDiagnosticsFor(R"cc(2747#include "unchecked_statusor_access_test_defs.h"2748 2749 void target(STATUSOR_INT sor) {2750 if (sor.status() != absl::InvalidArgumentError("oh no"))2751 sor.value(); // [[unsafe]]2752 else2753 sor.value(); // [[unsafe]]2754 }2755 )cc");2756 ExpectDiagnosticsFor(2757 R"cc(2758#include "unchecked_statusor_access_test_defs.h"2759 2760 void target(STATUSOR_INT x, STATUSOR_INT y) {2761 if (x.ok()) {2762 if (x.ok() == y.ok())2763 y.value();2764 else2765 y.value(); // [[unsafe]]2766 }2767 }2768 )cc");2769 ExpectDiagnosticsFor(R"cc(2770#include "unchecked_statusor_access_test_defs.h"2771 2772 void target(STATUSOR_INT x, STATUSOR_INT y) {2773 if (x.ok()) {2774 if (x.ok() != y.ok())2775 y.value(); // [[unsafe]]2776 else2777 y.value();2778 }2779 }2780 )cc");2781 ExpectDiagnosticsFor(2782 R"cc(2783#include "unchecked_statusor_access_test_defs.h"2784 2785 void target(STATUSOR_INT x, STATUSOR_INT y) {2786 if (x.ok()) {2787 if (x.status().ok() == y.status().ok())2788 y.value();2789 else2790 y.value(); // [[unsafe]]2791 }2792 }2793 )cc");2794 ExpectDiagnosticsFor(R"cc(2795#include "unchecked_statusor_access_test_defs.h"2796 2797 void target(STATUSOR_INT x, STATUSOR_INT y) {2798 if (x.ok()) {2799 if (x.status().ok() != y.status().ok())2800 y.value(); // [[unsafe]]2801 else2802 y.value();2803 }2804 }2805 )cc");2806 ExpectDiagnosticsFor(2807 R"cc(2808#include "unchecked_statusor_access_test_defs.h"2809 2810 void target(STATUSOR_INT x, STATUSOR_INT y) {2811 if (x.ok()) {2812 if (x.status().ok() == y.ok())2813 y.value();2814 else2815 y.value(); // [[unsafe]]2816 }2817 }2818 )cc");2819 ExpectDiagnosticsFor(R"cc(2820#include "unchecked_statusor_access_test_defs.h"2821 2822 void target(STATUSOR_INT x, STATUSOR_INT y) {2823 if (x.ok()) {2824 if (x.status().ok() != y.ok())2825 y.value(); // [[unsafe]]2826 else2827 y.value();2828 }2829 }2830 )cc");2831 ExpectDiagnosticsFor(R"cc(2832#include "unchecked_statusor_access_test_defs.h"2833 2834 void target(bool b, STATUSOR_INT sor) {2835 if (sor.ok() == b) {2836 if (b) sor.value();2837 }2838 }2839 )cc");2840 ExpectDiagnosticsFor(R"cc(2841#include "unchecked_statusor_access_test_defs.h"2842 2843 void target(STATUSOR_INT sor) {2844 if (sor.ok() == true) sor.value();2845 }2846 )cc");2847 ExpectDiagnosticsFor(R"cc(2848#include "unchecked_statusor_access_test_defs.h"2849 2850 void target(STATUSOR_INT sor) {2851 if (sor.ok() == false) sor.value(); // [[unsafe]]2852 }2853 )cc");2854 ExpectDiagnosticsFor(R"cc(2855#include "unchecked_statusor_access_test_defs.h"2856 2857 void target(bool b) {2858 STATUSOR_INT sor1;2859 STATUSOR_INT sor2 = Make<STATUSOR_INT>();2860 if (sor1 == sor2) sor2.value(); // [[unsafe]]2861 }2862 )cc");2863 ExpectDiagnosticsFor(R"cc(2864#include "unchecked_statusor_access_test_defs.h"2865 2866 void target(bool b) {2867 STATUSOR_INT sor1 = Make<STATUSOR_INT>();2868 STATUSOR_INT sor2;2869 if (sor1 == sor2) sor1.value(); // [[unsafe]]2870 }2871 )cc");2872}2873 2874TEST_P(UncheckedStatusOrAccessModelTest, PointerEqualityCheck) {2875 ExpectDiagnosticsFor(2876 R"cc(2877#include "unchecked_statusor_access_test_defs.h"2878 2879 void target(STATUSOR_INT* x, STATUSOR_INT* y) {2880 if (x->ok()) {2881 if (x == y)2882 y->value();2883 else2884 y->value(); // [[unsafe]]2885 }2886 }2887 )cc");2888 ExpectDiagnosticsFor(2889 R"cc(2890#include "unchecked_statusor_access_test_defs.h"2891 2892 void target(STATUSOR_INT* x, STATUSOR_INT* y) {2893 if (x->ok()) {2894 if (x != y)2895 y->value(); // [[unsafe]]2896 else2897 y->value();2898 }2899 }2900 )cc");2901 ExpectDiagnosticsFor(2902 R"cc(2903#include "unchecked_statusor_access_test_defs.h"2904 2905 void target(STATUS* x, STATUS* y) {2906 auto sor = Make<STATUSOR_INT>();2907 if (x->ok()) {2908 if (x == y && sor.status() == *y)2909 sor.value();2910 else2911 sor.value(); // [[unsafe]]2912 }2913 }2914 )cc");2915 ExpectDiagnosticsFor(2916 R"cc(2917#include "unchecked_statusor_access_test_defs.h"2918 2919 void target(STATUS* x, STATUS* y) {2920 auto sor = Make<STATUSOR_INT>();2921 if (x->ok()) {2922 if (x != y)2923 sor.value(); // [[unsafe]]2924 else if (sor.status() == *y)2925 sor.value();2926 }2927 }2928 )cc");2929}2930 2931TEST_P(UncheckedStatusOrAccessModelTest, Emplace) {2932 ExpectDiagnosticsFor(R"cc(2933#include "unchecked_statusor_access_test_defs.h"2934 2935 struct Foo {2936 Foo(int);2937 };2938 2939 void target(absl::StatusOr<Foo> sor, int value) {2940 sor.emplace(value);2941 sor.value();2942 }2943 )cc");2944 ExpectDiagnosticsFor(R"cc(2945#include "unchecked_statusor_access_test_defs.h"2946 2947 struct Foo {2948 Foo(std::initializer_list<int>, int);2949 };2950 2951 void target(absl::StatusOr<Foo> sor, int value) {2952 sor.emplace({1, 2, 3}, value);2953 sor.value();2954 }2955 )cc");2956 ExpectDiagnosticsFor(R"cc(2957#include "unchecked_statusor_access_test_defs.h"2958 2959 void target() {2960 STATUSOR_INT sor;2961 bool sor_ok = sor.ok();2962 if (!sor_ok)2963 sor.emplace(42);2964 sor.value();2965 }2966 )cc");2967 ExpectDiagnosticsFor(R"cc(2968#include "unchecked_statusor_access_test_defs.h"2969 2970 void target(bool b) {2971 STATUSOR_INT sor;2972 if (b) sor.emplace(42);2973 if (b) sor.value();2974 }2975 )cc");2976}2977 2978TEST_P(UncheckedStatusOrAccessModelTest, ValueConstruction) {2979 ExpectDiagnosticsFor(R"cc(2980#include "unchecked_statusor_access_test_defs.h"2981 2982 void target() {2983 STATUSOR_BOOL result = false;2984 result.value();2985 }2986 )cc");2987 ExpectDiagnosticsFor(R"cc(2988#include "unchecked_statusor_access_test_defs.h"2989 2990 void target() {2991 STATUSOR_INT result = 21;2992 result.value();2993 }2994 )cc");2995 ExpectDiagnosticsFor(R"cc(2996#include "unchecked_statusor_access_test_defs.h"2997 2998 void target() {2999 STATUSOR_INT result = Make<STATUSOR_INT>();3000 result.value(); // [[unsafe]]3001 }3002 )cc");3003 ExpectDiagnosticsFor(3004 R"cc(3005#include "unchecked_statusor_access_test_defs.h"3006 3007 void target() {3008 STATUSOR_BOOL result = false;3009 if (result.ok())3010 result.value();3011 else3012 result.value();3013 }3014 )cc");3015 3016 ExpectDiagnosticsFor(R"cc(3017#include "unchecked_statusor_access_test_defs.h"3018 3019 void target() {3020 STATUSOR_BOOL result(false);3021 result.value();3022 }3023 )cc");3024 ExpectDiagnosticsFor(R"cc(3025#include "unchecked_statusor_access_test_defs.h"3026 3027 void target() {3028 STATUSOR_INT result(21);3029 result.value();3030 }3031 )cc");3032 ExpectDiagnosticsFor(R"cc(3033#include "unchecked_statusor_access_test_defs.h"3034 3035 void target() {3036 STATUSOR_INT result(Make<STATUSOR_INT>());3037 result.value(); // [[unsafe]]3038 }3039 )cc");3040 ExpectDiagnosticsFor(3041 R"cc(3042#include "unchecked_statusor_access_test_defs.h"3043 3044 void target() {3045 STATUSOR_BOOL result(false);3046 if (result.ok())3047 result.value();3048 else3049 result.value();3050 }3051 )cc");3052}3053 3054TEST_P(UncheckedStatusOrAccessModelTest, ValueAssignment) {3055 ExpectDiagnosticsFor(R"cc(3056#include "unchecked_statusor_access_test_defs.h"3057 3058 void target() {3059 STATUSOR_BOOL result;3060 result = false;3061 result.value();3062 }3063 )cc");3064 ExpectDiagnosticsFor(R"cc(3065#include "unchecked_statusor_access_test_defs.h"3066 3067 void target() {3068 STATUSOR_INT result;3069 result = 21;3070 result.value();3071 }3072 )cc");3073 ExpectDiagnosticsFor(R"cc(3074#include "unchecked_statusor_access_test_defs.h"3075 3076 void target() {3077 STATUSOR_INT result;3078 result = Make<STATUSOR_INT>();3079 result.value(); // [[unsafe]]3080 }3081 )cc");3082 ExpectDiagnosticsFor(3083 R"cc(3084#include "unchecked_statusor_access_test_defs.h"3085 3086 void target() {3087 STATUSOR_BOOL result;3088 result = false;3089 if (result.ok())3090 result.value();3091 else3092 result.value();3093 }3094 )cc");3095}3096 3097TEST_P(UncheckedStatusOrAccessModelTest, NestedStatusOr) {3098 ExpectDiagnosticsFor(R"cc(3099#include "unchecked_statusor_access_test_defs.h"3100 3101 void target() {3102 absl::StatusOr<STATUSOR_INT> result;3103 result = Make<STATUSOR_INT>();3104 result.value();3105 }3106 )cc");3107 ExpectDiagnosticsFor(R"cc(3108#include "unchecked_statusor_access_test_defs.h"3109 3110 void target() {3111 absl::StatusOr<STATUSOR_INT> result = Make<STATUSOR_INT>();3112 result.value();3113 }3114 )cc");3115}3116 3117TEST_P(UncheckedStatusOrAccessModelTest, PtrConstruct) {3118 ExpectDiagnosticsFor(R"cc(3119#include "unchecked_statusor_access_test_defs.h"3120 3121 void target() {3122 STATUSOR_VOIDPTR sor = nullptr;3123 *sor;3124 }3125 )cc");3126 3127 ExpectDiagnosticsFor(R"cc(3128#include "unchecked_statusor_access_test_defs.h"3129 3130 void target() {3131 STATUSOR_VOIDPTR sor(nullptr);3132 *sor;3133 }3134 )cc");3135}3136 3137TEST_P(UncheckedStatusOrAccessModelTest, InPlaceConstruct) {3138 ExpectDiagnosticsFor(R"cc(3139#include "unchecked_statusor_access_test_defs.h"3140 3141 void target() {3142 STATUSOR_VOIDPTR absl_sor(absl::in_place, {nullptr});3143 *absl_sor;3144 STATUSOR_VOIDPTR std_sor(std::in_place, {nullptr});3145 *std_sor;3146 }3147 )cc");3148}3149 3150TEST_P(UncheckedStatusOrAccessModelTest, ConstructStatusOrFromReference) {3151 ExpectDiagnosticsFor(R"cc(3152#include "unchecked_statusor_access_test_defs.h"3153 void target() {3154 const auto sor1 = Make<STATUSOR_INT&>();3155 const auto sor2 = Make<STATUSOR_INT&>();3156 if (!sor1.ok() && !sor2.ok()) return;3157 if (sor1.ok() && !sor2.ok()) {3158 } else if (!sor1.ok() && sor2.ok()) {3159 } else {3160 sor1.value();3161 sor2.value();3162 }3163 }3164 )cc");3165}3166 3167TEST_P(UncheckedStatusOrAccessModelTest, ConstructStatusFromReference) {3168 ExpectDiagnosticsFor(R"cc(3169#include "unchecked_statusor_access_test_defs.h"3170 3171 void target() {3172 const auto sor1 = Make<STATUSOR_INT&>();3173 const auto sor2 = Make<STATUSOR_INT&>();3174 const auto s1 = Make<STATUS&>();3175 const auto s2 = Make<STATUS&>();3176 3177 if (!s1.ok() && !s2.ok()) return;3178 if (s1.ok() && !s2.ok()) {3179 } else if (!s1.ok() && s2.ok()) {3180 } else {3181 if (s1 != sor1.status() || s2 != sor2.status()) return;3182 sor1.value();3183 sor2.value();3184 }3185 }3186 )cc");3187}3188 3189} // namespace3190 3191std::string3192GetAliasMacros(UncheckedStatusOrAccessModelTestAliasKind AliasKind) {3193 switch (AliasKind) {3194 case UncheckedStatusOrAccessModelTestAliasKind::kUnaliased:3195 return R"cc(3196#define STATUSOR_INT ::absl::StatusOr<int>3197#define STATUSOR_BOOL ::absl::StatusOr<bool>3198#define STATUSOR_VOIDPTR ::absl::StatusOr<void*>3199#define STATUS ::absl::Status3200 )cc";3201 case UncheckedStatusOrAccessModelTestAliasKind::kPartiallyAliased:3202 return R"cc(3203 template <typename T>3204 using StatusOrAlias = ::absl::StatusOr<T>;3205#define STATUSOR_INT StatusOrAlias<int>3206#define STATUSOR_BOOL StatusOrAlias<bool>3207#define STATUSOR_VOIDPTR StatusOrAlias<void*>3208#define STATUS ::absl::Status3209 )cc";3210 case UncheckedStatusOrAccessModelTestAliasKind::kFullyAliased:3211 return R"cc(3212 using StatusOrIntAlias = ::absl::StatusOr<int>;3213#define STATUSOR_INT StatusOrIntAlias3214 using StatusOrBoolAlias = ::absl::StatusOr<bool>;3215#define STATUSOR_BOOL StatusOrBoolAlias3216 using StatusOrVoidPtrAlias = ::absl::StatusOr<void*>;3217#define STATUSOR_VOIDPTR StatusOrVoidPtrAlias3218 using StatusAlias = ::absl::Status;3219#define STATUS StatusAlias3220 )cc";3221 }3222 llvm_unreachable("Unknown alias kind.");3223}3224 3225std::vector<std::pair<std::string, std::string>>3226GetHeaders(UncheckedStatusOrAccessModelTestAliasKind AliasKind) {3227 auto Headers = test::getMockHeaders();3228 3229 Headers.emplace_back("unchecked_statusor_access_test_defs.h",3230 R"cc(3231#include "cstddef.h"3232#include "statusor_defs.h"3233#include "std_optional.h"3234#include "std_vector.h"3235#include "std_pair.h"3236#include "absl_log.h"3237#include "testing_defs.h"3238 3239 template <typename T>3240 T Make();3241 3242 class Fatal {3243 public:3244 ~Fatal() __attribute__((noreturn));3245 int value();3246 };3247 )cc" +3248 GetAliasMacros(AliasKind));3249 return Headers;3250}3251} // namespace clang::dataflow::statusor_model3252