dotnet / roslyn-analyzers

MIT License
1.59k stars 466 forks source link

CA2000 on using declaration with object initializer #2703

Closed sectax closed 5 years ago

sectax commented 5 years ago

Analyzer package

Microsoft.CodeAnalysis.FxCopAnalyzers

Package Version

v2.9.3 (Latest)

Diagnostic ID

CA2000

Repro steps

  1. Create a library using netstandard2.0 and <LangVersion>preview</LangVersion>
  2. Insert following snippet code
public static void RaisesWarning()
{
    using var aesEncryption = new RijndaelManaged
    {
        KeySize = 128
    };
}

public static void DoesNotRaiseWarning()
{
    using (var aesEncryption = new RijndaelManaged
    {
        KeySize = 128
    }) {}
}

Expected behavior

Either both methods generate CA2000, or neither.

Actual behavior

Only the method with a using declaration generates a CA2000 warning.

mavasani commented 5 years ago

This is due to missing IOperation support for using declarations in Roslyn: https://github.com/dotnet/roslyn/issues/32100

mavasani commented 5 years ago

I will add a workaround to the analyzer for this case.