dotnet / roslyn-analyzers

MIT License
1.55k stars 460 forks source link

RS2000 ignores target-typed new expressions and creation outside of immediate field-initialization #5957

Open ErikWe opened 2 years ago

ErikWe commented 2 years ago

Analyzer

Diagnostic ID: RS2000: Add analyzer diagnostic IDs to analyzer release

Analyzer source

SDK: Built-in CA analyzers in .NET 5 SDK or later

Version: SDK 6.0.201

NuGet Package: Microsoft.CodeAnalysis.Analyzers

Version: 3.3.3 (Latest)

Describe the bug

RS2000 is not displayed in two separate circumstances, despite the diagnostic ID not being listed in the analyzer release:

  1. A DiagnosticDescriptor is created using a 'target-typed new expression', i.e
    class Foo
    {
    DiagnosticDescriptor Rule = new(...);
    }
  2. A DiagnosticDescriptor is created outside of immediate field-initialization, e.g
    class Foo
    {
    DiagnosticDescriptor Rule { get; } = new DiagnosticDescriptor(...);
    }

However, creating a DiagnosticDescriptor in the following way results in RS2000:

class Foo
{
    DiagnosticDescriptor Rule = new DiagnosticDescriptor(...);
}

Expected behavior

If the diagnostic ID is not already listed in the analyzer release, RS2000 should be displayed - as is the case in the final example above.

Actual behavior

RS2000 is not displayed.

Additional information

Related diagnostics are affected in similar ways, such as RS1017.

Youssef1313 commented 2 years ago

Also https://github.com/dotnet/roslyn-analyzers/issues/5890

Regarding the property initializer case, I don't know if the behavior was intended or not. @mavasani Do you know if property initializers should be supported?