Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CA2000 reported on every using statement with IAsyncDisposable #6512

Closed
cytoph opened this issue Feb 28, 2023 · 1 comment · Fixed by #6787
Closed

CA2000 reported on every using statement with IAsyncDisposable #6512

cytoph opened this issue Feb 28, 2023 · 1 comment · Fixed by #6787
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

Comments

@cytoph
Copy link

cytoph commented Feb 28, 2023

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 use FileStream to show that the bug affects even the most common built-in types.

  1. Create an async method that contains a async using statement (like shown belown)
  2. See error
public async Task DoSomethingAsync()
{
    await using var stream = new FileStream("C:\somefile.txt", FileMode.OpenOrCreate).ConfigureAwait(false); // this already shows the false issue
}

Expected behavior

await using var stream = new FileStream("C:\somefile.txt", FileMode.OpenOrCreate).ConfigureAwait(false); will not report CA2000. Maybe if not used with a using it should report an issue, but that should be a newly created one, not CA2000, because CA2000 explicitly states one should call System.IDisposable.Dispose which is not correct for IAsyncDisposable.

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 a using 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 the finally part, which is exactly what the using 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? 🤷‍♂️

@alexrp
Copy link
Contributor

alexrp commented May 3, 2023

I'm also regularly running into this in a project using .NET 8 Preview 3.

@mavasani mavasani added Bug The product is not behaving according to its current intended design Area-Microsoft.CodeAnalysis.NetAnalyzers False_Positive A diagnostic is reported for non-problematic case help wanted The issue is up-for-grabs, and can be claimed by commenting good first issue labels Jun 23, 2023
@mavasani mavasani added this to the Unknown milestone Jun 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants