Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 9, 2026

TUnit0023 incorrectly warns that Func<IDisposable> fields should be disposed, but delegate types are not disposable—only the objects they create are.

public class ExampleTest
{
    private readonly Func<IMyInterface> _factory;

    public ExampleTest()
    {
        _factory = () => new MyClass(); // False positive: TUnit0023 warning
    }

    [Test]
    public void Test1()
    {
        using var t = _factory(); // Result is properly disposed here
    }
}

Changes

  • DisposableFieldPropertyAnalyzer.cs: CheckSetUps now verifies the field/property type is disposable before flagging, matching the existing logic in CheckFieldInitializers
  • DisposableFieldPropertyAnalyzerTests.cs: Added test coverage for Func<T> assignments in constructors and Before hooks
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:

public interface IMyInterface : IDisposable
{
}

public class MyClass : IMyInterface
{
    public void Dispose()
    {
        Console.WriteLine("disposed");
    }
}

public class ExampleTest
{
    private readonly Func<IMyInterface> _factory = () => new MyClass();

    [Test]
    public async Task Test1()
    {
        using var t = _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>

<agent_instructions>Read the latest comment in addition to the description for full context</agent_instructions>

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


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Fix compiler warning TUnit0023 for disposable variable Fix TUnit0023 false positive for Func<IDisposable> fields Jan 9, 2026
Copilot AI requested a review from thomhurst January 9, 2026 20:18
@thomhurst thomhurst marked this pull request as ready for review January 9, 2026 21:00
@thomhurst thomhurst merged commit 7d03318 into main Jan 9, 2026
11 of 13 checks passed
@thomhurst thomhurst deleted the copilot/fix-compiler-warning-tunit0023 branch January 9, 2026 21:00
This was referenced Jan 12, 2026
Sign up for free to 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

2 participants