CA2000 reported on every using statement with IAsyncDisposable #6512
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers
Bug
The product is not behaving according to its current intended design
False_Positive
A diagnostic is reported for non-problematic case
good first issue
help wanted
The issue is up-for-grabs, and can be claimed by commenting
Milestone
Analyzer
Diagnostic ID: CA2000:
Dispose objects before losing scope
Analyzer source
NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers
Version: 7.0.0 (Latest Stable)
(The bug also exists in all versions of the analyzer built-in into the SDK in 5.0, 6.0 and 7.0)
Describe the bug
Whenever one uses a using statement with an
IAsyncDisposable
the analyzer falsely reports CA2000.Steps To Reproduce
OPTIONAL: If you want, create a custom implementation of
IAsyncDisposable
. For this example I'll useFileStream
to show that the bug affects even the most common built-in types.Expected behavior
await using var stream = new FileStream("C:\somefile.txt", FileMode.OpenOrCreate).ConfigureAwait(false);
will not report CA2000. Maybe if not used with ausing
it should report an issue, but that should be a newly created one, not CA2000, because CA2000 explicitly states one should callSystem.IDisposable.Dispose
which is not correct forIAsyncDisposable
.Actual behavior
await using var stream = new FileStream("C:\somefile.txt", FileMode.OpenOrCreate).ConfigureAwait(false);
reports CA2000.IAsyncDisposable
in general reports this, no matter if used in ausing
or not. The only way to suppress the issue is to initialize a variable and after that start a try-finally-block where the variable is disposed in thefinally
part, which is exactly what theusing
is for in C#.Additional context
This bug was already reported in issue #3042 but no one bothered to re-open it even though it was not fixed - so what other way to call attention to this bug than creating a new issue? 🤷♂️
The text was updated successfully, but these errors were encountered: