>
>
>
V688. The 'foo' local variable has the …


V688. The 'foo' local variable has the same name as one of class members. This can result in confusion.

The analyzer has detected an issue when the name of a local variable coincides with the name of a class member. It is not an error in most cases, but such code may be EXTREMELY dangerous as it is exposed to errors that may occur after refactoring. The programmer assumes he is working with a class member while actually using the local variable.

An example of the error:

class M
{
  int x;
  void F() { int x = 1; foo(x); }
  ....
};

The class contains a member named 'x'. The same name is used for the local variable in the F() function.

The error is clearly seen in a small sample like that, so you may find the V688 diagnostic uninteresting. But when you work with large functions, such a careless choice of names for variables may cause much trouble to developers maintaining the code.

We just need to choose another name for the local variable to avoid the error:

class M
{
  int x;
  void F() { int value = 1; foo(value); }
  ....
};

Another solution is to use the 'm_' prefix in the names of class members:

class M
{
  int m_x;
  void F() { int x = 1; foo(x); }
  ....
};

The analyzer generates this warning in certain cases only. It employs certain heuristics mechanisms to avoid false positives. For example, it won't react to the following code:

class M
{
  int value;
  void SetValue(int value) { this->value = value; }
  ....
};

This diagnostic is classified as:

  • CERT-DCL01-C

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