>
>
>
V1007. Value from the uninitialized opt…


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);

Note. The diagnostic rule has a special setting that enables it to issue an extended message. The message contains a list of functions that need to be used to check an optional type object before obtaining the value. To display the extended message, add the following comment to the source code file or to the diagnostic rules configuration file (.pvsconfig):

//+V1007 PRINT_CHECKERS

This diagnostic is classified as:

You can look at examples of errors detected by the V1007 diagnostic.