Skip to content

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

@FlukeFan

Description

@FlukeFan

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

Metadata

Metadata

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions