# V6021. The value is assigned to the 'x' variable but is not used.

This diagnostic rule detects only cases when incorrect value is assigned to a variable which is reassigned with a new value or is not used at all.

Case 1.

One and the same variable is assigned a value twice. In addition, the variable itself is not used between these 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);

Case 2.

Local variable is assigned a value, but the variable is not used further anywhere until the exit of the method.

Consider the following example:

    String GetDisplayName(Titles titles, String name)
    {
      String result = null;
      String tmp = normalize(name);
      if (titles.isValidName(name, tmp)){
        result = name;
      }
      return name;
    }

The programmer wanted the method to return the 'result' variable, which gets initialized depending on how 'isValidName' executes, but made a typo that causes the method to return the variable 'name' all the time. The fixed code should look like this:

    String GetDisplayName(Titles titles, String name)
    {
      String result = null;
      String tmp = normalize(name);
      if (titles.isValidName(name, tmp)){
        result = name;
      }
      return result;
    }

This diagnostic is classified as:

CWE-563 CERT-MSC56-J

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