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.
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:
The
IDE0059
rule is not raised for the not used variablejsonContent
. But if we remove theusing
keyword:The
IDE0059
rule is raised to indicate that thejsonContent
variable is not used.We have the same issue if we use explicit code block for
using
keyword:Expected behavior
The
IDE0059
rule should be raised when a local variable is not used, even if there is a non-relatedusing
code block.