-
-
Notifications
You must be signed in to change notification settings - Fork 108
Fix TUnit0023 false positive for Func<IDisposable> fields #4247
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
Conversation
Co-authored-by: thomhurst <[email protected]>
Co-authored-by: thomhurst <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes a false positive in the TUnit0023 analyzer that incorrectly flagged Func<IDisposable> fields and properties as needing disposal. The issue occurred because the analyzer was checking if any object creation within an initializer created a disposable type, without verifying that the field/property type itself was disposable.
Key Changes:
- Added a secondary check to verify the field/property type itself implements
IDisposableorIAsyncDisposablebefore flagging it - Added comprehensive test coverage for
Func<IDisposable>patterns in fields and properties
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| TUnit.Analyzers/DisposableFieldPropertyAnalyzer.cs | Added checks to verify field and property types themselves are disposable before flagging them, fixing false positives for delegate types like Func<IDisposable> |
| TUnit.Analyzers.Tests/DisposableFieldPropertyAnalyzerTests.cs | Added three new tests covering Func<IDisposable> fields and properties with both interface and concrete disposable types |
TUnit0023 incorrectly flags
Func<IDisposable>fields as needing disposal, even though the delegate type itself is not disposable.Changes
CheckFieldInitializersnow verifies the field/property type itself implementsIDisposable/IAsyncDisposable, not just that the initializer contains a disposable object creationFunc<T>patterns (fields and properties)Root Cause
The analyzer was checking if any
ObjectCreationExpressionSyntaxin the initializer creates a disposable type, then flagging the containing field. This incorrectly flags delegate types where the disposable object is created inside a lambda—the delegate itself isn't disposable.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.