dotnet / roslyn-analyzers

MIT License
1.55k stars 460 forks source link

IDE0059 is not raised inside a code block with `using` keyword. #7339

Closed GillesTourreau closed 1 week ago

GillesTourreau commented 1 week ago

Analyzer

Diagnostic ID: IDE0059: Remove unnecessary value assignment

Analyzer source

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

Version: SDK 8.0.302

Describe the bug

IDE0059 rule is not raised inside a code block with the using keyword.

Steps To Reproduce

Write the following code:

public class Sandbox
{
    public void SimpleMethod()
    {
        using var _ = new MemoryStream();

        var jsonContent = JsonSerializer.Deserialize<Json>((JsonDocument)null);
    }

    public class Json
    {
    }
}

The IDE0059 rule is not raised for the not used variable jsonContent. But if we remove the using keyword:

public class Sandbox
{
    public void SimpleMethod()
    {
        var _ = new MemoryStream();

        var jsonContent = JsonSerializer.Deserialize<Json>((JsonDocument)null);
    }

    public class Json
    {
    }
}

The IDE0059 rule is raised to indicate that the jsonContent variable is not used.

We have the same issue if we use explicit code block for using keyword:

public class Sandbox
{
    public void SimpleMethod()
    {
        using var _ = new MemoryStream();
        {
            var jsonContent = JsonSerializer.Deserialize<Json>((JsonDocument)null);
        }
    }

    public class Json
    {
    }
}

Expected behavior

The IDE0059 rule should be raised when a local variable is not used, even if there is a non-related using code block.

GillesTourreau commented 1 week ago

Sorry, wrong repository project. I created the #74219 issue in the dotnet/roselyn project.