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
88 changes: 88 additions & 0 deletions TUnit.Analyzers.Tests/DisposableFieldPropertyAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1197,4 +1197,92 @@ public void Test1()
"""
);
}

// ========================================
// FUNC<IDISPOSABLE> SHOULD NOT BE FLAGGED
// ========================================

[Test]
public async Task 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 = () => new MyClass();

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

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

public class DisposableFieldTests
{
private readonly Func<HttpClient> _clientFactory = () => new HttpClient();

[Test]
public void Test1()
{
using var client = _clientFactory();
}
}
"""
);
}

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

public class DisposableFieldTests
{
private Func<HttpClient> ClientFactory { get; } = () => 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 @@ -112,7 +112,9 @@ private static void CheckFieldInitializers(SyntaxNodeAnalysisContext context, IN
if (typeInfo.Type?.IsDisposable() is true || typeInfo.Type?.IsAsyncDisposable() is true)
{
var fieldSymbol = context.SemanticModel.GetDeclaredSymbol(variable) as IFieldSymbol;
if (fieldSymbol != null)
// Only flag if the field type itself is disposable (not e.g. Func<IDisposable>)
if (fieldSymbol != null &&
(fieldSymbol.Type?.IsDisposable() is true || fieldSymbol.Type?.IsAsyncDisposable() is true))
{
createdObjects.TryAdd(fieldSymbol, HookLevel.Test);
break; // Only need to add once
Expand Down Expand Up @@ -145,7 +147,9 @@ private static void CheckFieldInitializers(SyntaxNodeAnalysisContext context, IN
if (typeInfo.Type?.IsDisposable() is true || typeInfo.Type?.IsAsyncDisposable() is true)
{
var propertySymbol = context.SemanticModel.GetDeclaredSymbol(propertyDeclaration) as IPropertySymbol;
if (propertySymbol != null)
// Only flag if the property type itself is disposable (not e.g. Func<IDisposable>)
if (propertySymbol != null &&
(propertySymbol.Type?.IsDisposable() is true || propertySymbol.Type?.IsAsyncDisposable() is true))
{
createdObjects.TryAdd(propertySymbol, HookLevel.Test);
break; // Only need to add once
Expand Down
Loading