# V3137. The variable is assigned but is not used by the end of the function.

The analyzer has detected a possible error that has to do with assigning a value to a local variable without ever using this variable before the method returns.

Consider the following example:

    private string GetDisplayName(string name)
    {
      MyStringId tmp = MyStringId.GetOrCompute(name);
      string result;
      if (!MyTexts.TryGet(tmp, out result))
        result = name;
      return name;
    }

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

    private string GetDisplayName(string name)
    {
      MyStringId tmp = MyStringId.GetOrCompute(name);
      string result;
      if (!MyTexts.TryGet(tmp, out result))
        result = name;
      return result;
    }

Consider another example:

    protected DateTimeOffset? GetFireTimeAfter()
    {
      DateTimeOffset sTime = StartTimeUtc;
      DateTimeOffset? time = null;
      ....
      if (....)
      {
        ....
        time = sTime;
      }
      else if (....)
      {
        ....
        time = sTime;
      }
      ....
      //apply the timezone before we return the time.
      sTime = TimeZoneUtil.ConvertTime(time.Value, this.TimeZone);
      return time;
    }

In several 'if' blocks, the 'time' variable is assigned the value 'sTime' storing some initial time incremented by a certain interval. The 'time' variable is returned at the end of the method. Before that, as suggested by the comment, the programmer wants to adjust the time depending on the time zone. Because of a typo, what is adjusted instead is the time zone of the 'sTime' variable, which is not used anymore. The correct version should probably look like this:

    protected DateTimeOffset? GetFireTimeAfter()
    {
      DateTimeOffset sTime = StartTimeUtc;
      DateTimeOffset? time = null;
      ....
      //apply the timezone before we return the time.
      time = TimeZoneUtil.ConvertTime(time.Value, this.TimeZone);
      return time;
    }

It is a common practice to assign some value to a variable at declaration even though it is not used anywhere after that. This is usually not a mistake: for example, declaring a variable in this way may be prescribed by the coding standard at your company, which requires storing the return result of any method in a variable even if this result is not used in any way. For example:

    void SomeMethod()
    {
      ....
      int result = DoWork();
      ....
    }

The analyzer provides for such situations and will not issue the warning in those cases.

This diagnostic is classified as:

CWE-563

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