dotnet/roslyn

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

Open

#74,219 创建于 2024年7月1日

在 GitHub 查看
 (0 评论) (0 反应) (1 负责人)C# (4,257 fork)batch import
Area-IDEBugFeature - IDE0059help wanted

仓库指标

Star
 (20,414 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 256 个 PR)

描述

Version Used: SDK 8.0.302 (4.11.0-2.final

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
    {
    }
}

Diagnostic Id: IDE0059: Remove unnecessary value assignment

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.

贡献者指南