>
>
>
V6047. It is possible that this 'else' …


V6047. It is possible that this 'else' branch must apply to the previous 'if' statement.

Analyzer detected a potential error in logical conditions: code's logic does not coincide with the code formatting.

Consider this sample:

if (X)
  if (Y) Foo();
else
  z = 1;

The code formatting disorientates you so it seems that the "z = 1" assignment takes place if X == false. But the 'else' branch refers to the nearest operator 'if'. In other words, this code is actually analogous to the following code:

if (X)
{
    if (Y)
      Foo();
    else
      z = 1;
}

So, the code does not work the way it seems at first sight.

If you get the V6047 warning, it may mean one of the two following things:

1) Your code is badly formatted and there is no error actually. In this case you need to edit the code so that it becomes clearer and the V6047 warning is not generated. Here is a sample of correct editing:

if (X)
  if (Y)
    Foo();
  else
    z = 1;

2) A logical error has been found. Then you may correct the code, for instance, this way:

if (X) {
  if (Y)
    Foo();
} else {
  z = 1;
}

This diagnostic is classified as: