stryker-net icon indicating copy to clipboard operation
stryker-net copied to clipboard

Use of unassigned local variable

Open ashahabov opened this issue 3 years ago • 2 comments
trafficstars

Describe the bug The Stryker raises "Use of unassigned local variable..." warning message and asks to report this on GitHub.

Logs

[12:00:21 WRN] Stryker.NET encountered an compile error in c:\Documents\ShapeCrawler\Code\ShapeCrawler\Charts\IChartPointCollection.cs (at 40:100) with message: Use of unassigned local variable 'numReference' (Source code: numReference) [12:00:21 WRN] Safe Mode! Stryker will try to continue by rolling back all mutations in method. This should not happen, please report this as an issue on github with the previous error message.

Expected behavior That file can be mutated.

Desktop (please complete the following information):

  • OS: Windows 10
  • Type of project: Library
  • Framework Version: .NET 5
  • Stryker Version: 1.2.1

Additional context

The source code:

public static ChartPointCollection Create(SCChart chart, OpenXmlElement cSerXmlElement)
{
    DocumentFormat.OpenXml.Drawing.Charts.NumberReference numReference;
    DocumentFormat.OpenXml.Drawing.Charts.Values? cVal = cSerXmlElement.GetFirstChild<DocumentFormat.OpenXml.Drawing.Charts.Values>();
    if (cVal != null)
    {
        // Some charts do have <c:val> element, for example, scatter chart.
        numReference = cVal.NumberReference!;
    }
    else
    {
        numReference = cSerXmlElement.GetFirstChild<DocumentFormat.OpenXml.Drawing.Charts.YValues>()!.NumberReference!;
    }

    IReadOnlyList<double> pointValues = ChartReferencesParser.GetNumbersFromCacheOrWorkbook(numReference, chart);
    List<ChartPoint> points = new();
    foreach (double point in pointValues)
    {
        points.Add(new ChartPoint(point));
    }

    return new ChartPointCollection(points);
}

The source code with line numbers:

image

ashahabov avatar Dec 26 '21 11:12 ashahabov

To add to this it appears to struggle with Type-testing operators: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/type-testing-and-cast

namely: if (thing is IInterface newTypeCheckedVariable) will result in the same error.

alex-irvine avatar Feb 28 '22 18:02 alex-irvine

Facing the same problem on 1.5.0

Use of unassigned local variable 'typeName' (Source code: typeName)

public static string GetGenericTypeName(this Type type)
{
    string typeName;

    if (type.IsGenericType)
    {
        var genericTypes = string.Join(",", type.GetGenericArguments().Select(t => t.Name).ToArray());
        typeName =
            $"{type.Name.Remove(type.Name.IndexOf('`', StringComparison.OrdinalIgnoreCase))}<{genericTypes}>";
    }
    else
    {
        typeName = type.Name;
    }

    return typeName;
}

klemmchr avatar Mar 23 '22 22:03 klemmchr