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
19.02k stars 4.03k forks source link

Use 'Throw' expression causes erroneous code #19507

Closed znakeeye closed 7 years ago

znakeeye commented 7 years ago

Version Used: 15.2 (26430.4)

Steps to Reproduce:

public Foo(T bar)
{
    if (bar == null)  // Use 'Throw' expression here...
    {
        throw new ArgumentNullException(nameof(bar));
    }
}

Error CS0019 Operator '??' cannot be applied to operands of type 'T' and '\'

However, if I add a constraint on T in my class it works:

public sealed class Foo<TClass> where TClass : class
{}

Expected Behavior: The "Use Throw expression" command should not be available or it should complain about the type. Maybe suggest adding the "where"-constraint above?

Actual Behavior: Compilation error CS0019.

sharwell commented 7 years ago

It appears our analyzer is ignoring the rule described in #17754.

Pilchie commented 7 years ago

Already fixed for 15.3 as part of https://github.com/dotnet/roslyn/pull/18178.