S1481 should not apply to local variables declared with the "using" keyword

The following generates a warning that the local variable stream is unused, but when the using keyword is present, the _ discard cannot be used.

void Foo(string path)
{
    using var stream = File.Create(path);
}

The following fails to compile:

using File.Create(path);

And the following (correctly) generates a different sonar warning that the {} is empty

using (File.Create(path)) {}

Hi @paulhickman,

I can confirm the false positive. I’ve created an issue for this and you can follow the progress here: https://github.com/SonarSource/sonar-dotnet/issues/3137

Thanks for letting us know,
Costin

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.