V1007. Value from the uninitialized optional is used. It may be an error.
The analyzer has detected an attempt to access the value of an object of class optional
, which was not previously initialized, so it does not store any value. Technically, this issue leads to undefined behavior and gives rise to other errors.
Look at 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 is never initialized, which prevents the code in the if (opt)
branch from executing.
The fixed code:
std::optional<Value> opt;
if (cond)
{
opt = Value(10, 20);
}
if (opt)
{
....
}
The analyzer can also detect cases 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);
The fixed code:
boost::optional<int> opt = boost::none;
opt = interpret(tr);
if (!cond)
process(*opt);
Note. The diagnostic rule has additional settings. To enable them, add a special comment to the source code file or to the diagnostic rule configuration file (.pvsconfig).
Extended message. This setting enables an extended message that includes a list of functions to check an optional type object before obtaining its value:
//+V1007 PRINT_CHECKERS
Pedantic mode. When this setting is enabled, the analyzer generates a V1007 warning whenever an unverified optional type object is used.
//+V1007 PEDANTIC_MODE
This diagnostic is classified as:
|
You can look at examples of errors detected by the V1007 diagnostic. |