Fix TUnit0023 false positive for Func<IDisposable> fields#4247
Merged
Fix TUnit0023 false positive for Func<IDisposable> fields#4247
Conversation
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix compiler warning TUnit0023 for IDisposable variable
Fix TUnit0023 false positive for Func<IDisposable> fields
Jan 6, 2026
There was a problem hiding this comment.
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 |
This was referenced Jan 6, 2026
This was referenced Feb 4, 2026
This was referenced Feb 12, 2026
This was referenced Feb 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.