Closed
Description
I'm trying to upgrade to clang-tidy-18 and found a regression with the changed bugprone-unchecked-optional-access check.
I managed to distill the following minimal reproducer:
#include <iostream>
#include <optional>
std::optional<int> get_optional()
{
return 5;
}
int main()
{
for (int j = 0; j < 1; j++)
{
}
auto i = get_optional();
// perform the check
if (!i.has_value())
{
return 0;
}
for (int j = 0; j < 1; ++j)
{
// test.cpp:24:19: error: unchecked access to optional value [bugprone-unchecked-optional-access,-warnings-as-errors]
// 24 | std::cout << *i << std::endl;
std::cout << *i << std::endl;
}
return 0;
}
The reuse of the loop indexing variable name seems to be the root cause.