V3008. The 'x' variable is assigned values twice successively. Perhaps this is a mistake.


The analyzer has detected an error that has to do with assigning values to one and the same variable twice in a row, while this variable is not used in any way between the assignments.

Consider this example:

A = GetA();
A = GetB();

The 'A' variable being assigned values twice might indicate a bug. The code should have most probably looked like this:

A = GetA();
B = GetB();

Cases when the variable is used between the assignments are treated as correct and do not trigger the warning:

A = 1;
A = Foo(A);

The following is an example of the bug taken from a real-life application:

....
if (bool.TryParse(setting, out value))
    _singleSignOn = value;
_singleSignOn = false;
....

A correct version of this code should look like this:

....
if (bool.TryParse(setting, out value))
    _singleSignOn = value;
else
    _singleSignOn = false;
....

The analyzer might output false positives sometimes. This happens when such variable assignments are used for debugging purposes. For example:

status = Foo1();
status = Foo2();

The false positive in this code can be handled in a number of ways:

  • You can suppress it by inserting comment "//-V3008".
  • You can forbid the analyzer to output diagnostic V3008 for any case where variable 'status' is used. To do that, insert comment "//-V:status:3008".
  • You can remove idle assignments from the code.
  • Perhaps this code is incorrect, so we have to check the value of the 'status' variable.

According to Common Weakness Enumeration, potential errors found by using this diagnostic are classified as CWE-563.

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


Bugs Found

Checked Projects
346
Collected Errors
13 188