Skip to content

Fix TUnit0023 false positive for Func<IDisposable> fields - #4247

Merged
thomhurst merged 3 commits into
mainfrom
copilot/fix-compiler-warning-tunit0023
Jan 6, 2026
Merged

Fix TUnit0023 false positive for Func<IDisposable> fields#4247
thomhurst merged 3 commits into
mainfrom
copilot/fix-compiler-warning-tunit0023

Conversation

CopilotAI commented Jan 6, 2026

Copy link
Copy Markdown
Contributor

TUnit0023 incorrectly flags Func<IDisposable> fields as needing disposal, even though the delegate type itself is not disposable.

publicclassExampleTest{// Incorrectly triggers: "TUnit0023: _factory should be disposed within a clean up method"privatereadonlyFunc<IMyInterface>_factory=()=>newMyClass();[Test]publicasyncTaskTest1(){usingvart=_factory();// Caller handles disposal correctly}}

Changes

  • DisposableFieldPropertyAnalyzer.cs: CheckFieldInitializers now verifies the field/property type itself implements IDisposable/IAsyncDisposable, not just that the initializer contains a disposable object creation
  • DisposableFieldPropertyAnalyzerTests.cs: Added test coverage for Func<T> patterns (fields and properties)

Root Cause

The analyzer was checking if any ObjectCreationExpressionSyntax in 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

This section details on the original issue you should resolve

<issue_title>[Bug]: Compiler warning TUnit0023 for variable that cannot be disposed</issue_title>
<issue_description>### Description

The warning TUnit0023 is given for a member variable of type Func<IDisposable>, but the member variable itself cannot be disposed of.

Expected Behavior

I would not expect a compiler warning.

Actual Behavior

Compiling the below code results in the warning error TUnit0023: _factory should be disposed within a clean up method.

Steps to Reproduce

Compile the following code in a TUnit project:

publicinterfaceIMyInterface:IDisposable{}publicclassMyClass:IMyInterface{publicvoidDispose(){Console.WriteLine("disposed");}}publicclassExampleTest{privatereadonlyFunc<IMyInterface>_factory=()=>newMyClass();[Test]publicasyncTaskTest1(){usingvart=_factory();}}

TUnit Version

1.9.2

.NET Version

.NET 10

Operating System

Windows

IDE / Test Runner

dotnet CLI (dotnet test / dotnet run)

Error Output / Stack Trace

error TUnit0023: _factory should be disposed within a clean up method

Additional Context

Image

IDE-Specific Issue?

  • I've confirmed this issue occurs when running via dotnet test or dotnet run, not just in my IDE</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 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.

CopilotAIand others added 2 commits January 6, 2026 19:52
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
CopilotAI changed the title [WIP] Fix compiler warning TUnit0023 for IDisposable variableFix TUnit0023 false positive for Func<IDisposable> fieldsJan 6, 2026
CopilotAI requested a review from thomhurstJanuary 6, 2026 19:59
@thomhurst
thomhurst marked this pull request as ready for review January 6, 2026 20:41
CopilotAI review requested due to automatic review settings January 6, 2026 20:41
@thomhurst
thomhurst enabled auto-merge (squash) January 6, 2026 20:41

CopilotAI left a comment

Copy link
Copy Markdown

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 IDisposable or IAsyncDisposable before 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.

FileDescription
TUnit.Analyzers/DisposableFieldPropertyAnalyzer.csAdded 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.csAdded three new tests covering Func<IDisposable> fields and properties with both interface and concrete disposable types

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Compiler warning TUnit0023 for variable that cannot be disposed

3 participants

@thomhurst