The following code triggers cpp:S6023 (Simplify this code by using “std::optional” member function “value_or”)
int demo(std::optional<int> x, int y)
{
if (x.has_value())
return x.value();
else
return slow_calculation(y);
}
This seems not to take into account that value_or
is a normal function so the argument is evaluated eagerly: replacing the function with
int demo(std::optional<int> x, int y)
{
return x.value_or(slow_calculation(y));
}
would end up running the slow_calculation(y)
even on the path where x
has a value.
I believe this rule should be adjusted so it only fires if the argument to value_or()
is sufficiently trivial, e.g. an integer literal, something that moving/copying does not require heap memory allocations, etc.
The above code would be a candidate for use of or_else
in C++23, but for C++17/C++20, the if
statement is in my opinion the better approach.
Thoughts/workarounds?