Our website uses cookies to enhance your browsing experience.
Accept
to the top

Webinar: Let's make a programming language. Lexer - 29.04

>
>
>
V570. Variable is assigned to itself.
menu mobile close menu
Additional information
toggle menu Contents

V570. Variable is assigned to itself.

Aug 08 2019

The analyzer has detected a potential error: a variable is assigned to itself.

The example:

dst.m_a = src.m_a;
dst.m_b = dst.m_b;

The dst.m_b variable value remains unchanged because of a typo.

The fixed code:

dst.m_a = src.m_a;
dst.m_b = src.m_b;

The analyzer issues a warning not only for copy assignment but also for move assignment.

dst.m_a = std::move(src.m_a);

The analyzer does not issue the warning every time it detects assignment of a variable to itself. For example, if the variables are enclosed in parentheses. This method is often used to suppress compiler-generated warnings.

The example:

int Foo(int foo)
{
  UNREFERENCED_PARAMETER(foo);
  return 1;
}

The UNREFERENCED_PARAMETER macro is defined in the WinNT.h file in the following way:

#define UNREFERENCED_PARAMETER(P)          \
  { \
      (P) = (P); \
  }

The analyzer recognizes such cases and does not issue the V570 warning for assignment like this:

(foo) = (foo);

If this approach is not used in the project, add the following comment to enable the warning:

//V_WARN_ON_ARGUMENT_SELF_ASSIGN

Note. If the V570 warning is issued for macros that cannot be changed, use macro suppression via adding special comment in a common project file (for instance, StdAfx.h).

The example:

//-V:MY_MACROS:V570

This diagnostic is classified as:

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