V561. It's probably better to assign value to 'foo' variable than to declare it anew

04.02.2011

The analyzer detected a potential error: there is a variable in code which is defined and initialized but not being used further. Besides, there is a variable in the exterior scope which also has the same name and type. It is highly probable that the programmer intended to use an already existing variable instead of defining a new one.

Let's examine this sample:

BOOL ret = TRUE;
if (m_hbitmap)
  BOOL ret = picture.SaveToFile(fptr);

The programmer defined a new variable 'ret' by accident, which causes the previous variable to always have the TRUE value regardless if the picture is saved into a file successfully or not. This is the correct code:

BOOL ret = TRUE;
if (m_hbitmap)
  ret = picture.SaveToFile(fptr);
You can look at examples of errors from real projects which were detected by this diagnostic message.