V1007. Value from the uninitialized optional is used. It may be an error.
The analyzer has detected an issue that has to do with accessing the value of an object of class 'optional', which was not previously initialized, that is, does not store any value. Technically, this issue leads to undefined behavior and gives rise to other errors.
Consider the following example of incorrect code:
std::optional<Value> opt;
if (cond)
{
opt->max = 10;
opt->min = 20;
}
if (opt)
{
....
}
In this example, the 'opt' variable was never initialized, which, in its turn, prevents the code in the "if (opt)" branch from executing.
Fixed version:
std::optional<Value> opt;
if (cond)
{
opt = Value(10, 20);
}
if (opt)
{
....
}
The analyzer can also detect situations where the value of a potentially uninitialized object of type 'optional' is accessed. For example:
boost::optional<int> opt = boost::none;
opt = interpret(tr);
if (cond)
opt = {};
process(*opt);
Fixed version:
boost::optional<int> opt = boost::none;
opt = interpret(tr);
if (!cond)
process(*opt);
This diagnostic is classified as:
|
You can look at examples of errors detected by the V1007 diagnostic. |