Issue 181737
Summary [clang-tidy] `bugprone-unchecked-optional-access` warns after `ASSERT_TRUE(optional.has_value())` in GoogleTest
Labels clang-tidy
Assignees
Reporter Stee-T
    **Description**

`bugprone-unchecked-optional-access` emits a warning when accessing an `std::optional` value inside a GoogleTest unit test, even if the optional is checked using a fatal GoogleTest assertion that terminates the test on failure.

Because `ASSERT_*` macros abort the current test function when the condition is false, execution cannot reach the `.value()` call unless the optional contains a value. Therefore this access should be considered guarded.

**Minimal example**

```cpp
#include <optional>
#include <gtest/gtest.h>

TEST( Example, OptionalChecked ) {
 std::optional< int > MyOptional = 42069;

  ASSERT_TRUE( MyOptional.has_value() );  // Fatal: aborts test if false
  EXPECT_EQ( MyOptional.value(), 42069 ); // clang-tidy warns here
}
```

**Actual behavior**

clang-tidy reports an "unchecked access to optional value" warning on the `.value()` call.

**Expected behavior**

No warning should be emitted after a fatal GoogleTest assertion that checks `has_value()`, since control flow cannot continue when the condition is false.

**Suggested improvement**

Treat GoogleTest fatal assertions (e.g. `ASSERT_TRUE(optional.has_value())`) as control-flow-terminating guards, similar to how other terminating constructs are modeled.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to