dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.71k stars 3.98k forks source link

false-positive CS8631 and CS8634 #68773

Open KryuchkovAlexandr opened 1 year ago

KryuchkovAlexandr commented 1 year ago

Version Used: Visual Studio 2022 v17.6.4, .NET7

Steps to Reproduce:

public class Program
{
    interface IModel { }
    class ConcreteModel : IModel { }

    public void Method()
    {
        var sourceModel = new ConcreteModel();

        // CS8631 and CS8634 warnings
        Translate(sourceModel, out _);

        // no warnings
        Translate(sourceModel, out var unused);
    }

    private void Translate<T>(T source, out T translated) where T : class, IModel
    {
        translated = source;
    }
}

Diagnostic Id: CS8631, CS8634

Expected Behavior: No warnings.

Actual Behavior: Using discards triggers CS8631 and CS8634.

PhilipCavanagh commented 6 months ago

I just came here to report a very similar bug, but it seems to boil down to the same thing:

internal static class Program
{
    private static void Main()
    {        
        ThrowIfNull((string?)null, out _); // CS8634
    }

    private static void ThrowIfNull<T>(T? input, out T output) where T : class
    {
        output = input ?? throw new ArgumentNullException(nameof(input));
    }
}