Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Potentially empty optional 'variable' is unwrapped, may throw exception
Remarks
Unwrapping empty std::optional values via the value method will throw an exception. Such operation can result in a crash when the exception isn't handled. This check will attempt to find cases where a std::optional isn't checked for emptiness before unwrapping it via the value method. You can enable C26829, and C26859 only for a more permissive analysis.
Example
std::optional<int> getOptional();
void f(std::optional<int> maybeEmpty)
{
if (maybeEmpty)
maybeEmpty.value() = 42; // No warning
maybeEmpty.value() = 5; // warning: C26860
std::optional<int> o = getOptional();
o.value() = 42; // warning: C26860
}
To solve this problem, make sure the code never unwraps an empty optional.