stryker-mutator / stryker-net

Mutation testing for .NET core and .NET framework!
https://stryker-mutator.io
Apache License 2.0
1.79k stars 188 forks source link

Use of unassigned local variable #1845

Open ashahabov opened 2 years ago

ashahabov commented 2 years ago

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):

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

alex-irvine commented 2 years ago

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.

klemmchr commented 2 years ago

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;
}