﻿# 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:

```cpp
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:

```cpp
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:

```cpp
boost::optional<int> opt = boost::none;
opt = interpret(tr);
    
if (cond)
  opt = {};
    
process(*opt);
```

The fixed code:

```cpp
boost::optional<int> opt = boost::none;
opt = interpret(tr);
    
if (!cond)
  process(*opt);
```

**Note**\. The diagnostic rule has additional settings\. To enable them, [add](https://pvs-studio.com/en/docs/manual/0040/) 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:

```cpp
//+V1007 PRINT_CHECKERS
```

**Pedantic mode\.** When this setting is enabled, the analyzer generates a V1007 warning whenever an unverified optional type object is used\.

```cpp
//+V1007 PEDANTIC_MODE
```