-
Notifications
You must be signed in to change notification settings - Fork 470
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
CA2213 Should Not Give False Positives for classes implementing IAsyncDisposable #6703
Comments
@TheCakeMonster This issue repros only if your |
@mavasani Thanks for checking, and for the additional information. I think the format/structure of this code section was generated for me by Visual Studio; I don't think I would have made the method virtual by myself. In other words, I think there is an analyzer fix associated with the IAsyncDisposable interface (implement interface?) that generates the code that causes the false positive. It feels like there is a disconnect between the fix and the analyzer checking its results if VS generates code that the analyzer is unable to successfully check. It is possible that I copied the code from the fix for the sync method and then modified it I suppose, but I don't think so. The point is that I thought I had used the recommended "implement using the Dispose pattern" approach in this code, so it seems a little odd that it results in a build warning. |
When implementing according to the guidelines of dotnet as described in https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-disposeasync I am also running into this false positive. The following class structure is proposed on the page for a non sealed class:
Because it is a non sealed class the DisposeAsyncCore method must be virtual. Furthermore the virtual DisposeAsyncCore method is always called by the public DisposeAsync in this pattern. Therefore all Child classes will call the virtual method when DisposeAsync is called. |
After some further testing it seems to only go wrong in case of implementing IAsyncDisposable without implement IDisposable.
Since https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-disposeasync describes that a class in specific case may implement only IAsyncDisposable (without implementing IDisposable). This is still a false positive of the analyzer. |
@JorisCleVR Is your sample fixed by #6976 or not? Or is it somehow different? |
@MartyIX As far as I can see I think it should indeed also solve this issue. That would be great! Thank you for fixing it. |
Note that the |
Analyzer
Diagnostic ID: CA2213
Describe the improvement
Classes that implement IAsyncDisposable along with the async dispose pattern can suffer from false positives of CA2213 when all analyzers are enabled, because the disposal of items in a virtual DisposeAsync method is not recognized.
Example Code
Here is a class that demonstrates the issue on my machine. Create a .NET 6 Console application and add this class. The false positive is from the CancellationTokenSource, which the analyzer thinks is not being disposed.
VS 2022 17.6.4
SDK 7.0.304
The following are the settings from the csproj that enable the analyzer and result in these false positives:
Additional context
As you can see from the csproj, we use an analysis mode that increases the number of analyzers that run. This issue does not show if you do not specify that a higher level of analysis should be performed. When validating and working on this issue, be sure to specify these csproj settings.
The text was updated successfully, but these errors were encountered: