>
>
>
V593. Expression 'A = B == C' is calcul…


V593. Expression 'A = B == C' is calculated as 'A = (B == C)'. Consider inspecting the expression.

The analyzer detected a potential error in an expression that is most probably working in a way other than intended by the programmer. Most often you may see errors of this type in expressions where an assignment operation and operation of checking a function's result are performed simultaneously.

Consider a simple example:

if (handle = Foo() != -1)

While creating this code, the programmer usually wants the actions to be performed in the following order:

if ((handle = Foo()) != -1)

But the priority of the '!=' operator is higher than that of the '=' operator. That is why the expression will be calculated in the following way:

if (handle = (Foo() != -1))

To fix the error, you may use parentheses, or rather not be stingy with code lines. Your program's text will become more readable if you write it this way:

handle = Foo();
if (handle != -1)

Let's see how such an error might look in a real application:

if (hr = AVIFileGetStream(pfileSilence,
           &paviSilence, typeAUDIO, 0) != AVIERR_OK)
{
  ErrMsg("Unable to load silence stream");
  return hr; 
}

The check in the code where the error has occurred works correctly and we will get the message "Unable to load silence stream". The trouble is that the 'hr' variable will store value 1 and not the error's code. This is the fixed code:

if ((hr = AVIFileGetStream(pfileSilence,
           &paviSilence, typeAUDIO, 0)) != AVIERR_OK)
{
  ErrMsg("Unable to load silence stream");
  return hr; 
}

The analyzer does not always generate warnings on detecting a construct of the "if (x = a == b)" kind. For instance, the analyzer understands that the following code is safe:

char *from;
char *to;
bool result;
...
if (result = from == to)
{}

Note. If the analyzer still generates a false alarm, you may use two methods to suppress it:

1) Add one more pair of parentheses. For example: "if (x = (a == b))".

2) Use a comment to suppress the warning. For example: "if (x = a == b) //-V593".

This diagnostic is classified as:

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