Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions TUnit.Analyzers.Tests/DisposableFieldPropertyAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,4 +1285,75 @@ public void Test1()
"""
);
}

[Test]
public async Task Constructor_Func_Returning_Disposable_No_Issue()
{
await Verifier
.VerifyAnalyzerAsync(
"""
using System;
using System.Net.Http;
using TUnit.Core;

public interface IMyInterface : IDisposable
{
}

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

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

public ExampleTest()
{
_factory = () => new MyClass();
}

[Test]
public void Test1()
{
using var t = _factory();
}
}
"""
);
}

[Test]
public async Task BeforeTest_Func_Returning_Disposable_No_Issue()
{
await Verifier
.VerifyAnalyzerAsync(
"""
using System;
using System.Net.Http;
using TUnit.Core;

public class DisposableFieldTests
{
private Func<HttpClient>? _clientFactory;

[Before(HookType.Test)]
public void Setup()
{
_clientFactory = () => new HttpClient();
}

[Test]
public void Test1()
{
using var client = _clientFactory!();
}
}
"""
);
}
}
8 changes: 6 additions & 2 deletions TUnit.Analyzers/DisposableFieldPropertyAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,18 @@ private static void CheckSetUps(SyntaxNodeAnalysisContext context, IMethodSymbol
.OfType<IObjectCreationOperation>()
.Any(x => x.Type?.IsDisposable() is true || x.Type?.IsAsyncDisposable() is true))
{
// Only flag if the field type itself is disposable (not e.g. Func<IDisposable>)
if (assignmentOperation.Target is IFieldReferenceOperation fieldReferenceOperation
&& context.Compilation.HasImplicitConversion(methodSymbol.ContainingType, fieldReferenceOperation.Field.ContainingType))
&& context.Compilation.HasImplicitConversion(methodSymbol.ContainingType, fieldReferenceOperation.Field.ContainingType)
&& (fieldReferenceOperation.Field.Type?.IsDisposable() is true || fieldReferenceOperation.Field.Type?.IsAsyncDisposable() is true))
{
createdObjects.TryAdd(fieldReferenceOperation.Field, level);
}

// Only flag if the property type itself is disposable (not e.g. Func<IDisposable>)
if (assignmentOperation.Target is IPropertyReferenceOperation propertyReferenceOperation
&& context.Compilation.HasImplicitConversion(methodSymbol.ContainingType, propertyReferenceOperation.Property.ContainingType))
&& context.Compilation.HasImplicitConversion(methodSymbol.ContainingType, propertyReferenceOperation.Property.ContainingType)
&& (propertyReferenceOperation.Property.Type?.IsDisposable() is true || propertyReferenceOperation.Property.Type?.IsAsyncDisposable() is true))
{
createdObjects.TryAdd(propertyReferenceOperation.Property, level);
}
Expand Down
Loading