V688. The 'foo' local variable possesses the same name as one of the class members, which can result in a 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; }
  ....
};


Do you make errors in the code?

Check your code
with PVS-Studio

Static code analysis
for C, C++, and C#

goto PVS-Studio;
We use cookies for the analysis of events to improve our content and make user interaction more convenient. By continuing the view of our web-pages you accept the terms of using these files. You can find out more about cookie-files and privacy policy or close the notification, by clicking on the button. Learn More →
Do not show