Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positive for PreferTestCleanupOverDispose on non-test classes #4378

Merged
merged 1 commit into from
Dec 18, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,28 @@ public override void Initialize(AnalysisContext context)

context.RegisterCompilationStartAction(context =>
{
if (context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemIDisposable, out INamedTypeSymbol? idisposableSymbol))
if (context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemIDisposable, out INamedTypeSymbol? idisposableSymbol) &&
context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftVisualStudioTestToolsUnitTestingTestClassAttribute, out INamedTypeSymbol? testClassAttributeSymbol))
{
INamedTypeSymbol? iasyncDisposableSymbol = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemIAsyncDisposable);
INamedTypeSymbol? valueTaskSymbol = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemThreadingTasksValueTask);
context.RegisterSymbolAction(context => AnalyzeSymbol(context, idisposableSymbol, iasyncDisposableSymbol, valueTaskSymbol), SymbolKind.Method);
context.RegisterSymbolAction(context => AnalyzeSymbol(context, testClassAttributeSymbol, idisposableSymbol, iasyncDisposableSymbol, valueTaskSymbol), SymbolKind.Method);
}
});
}

private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol idisposableSymbol, INamedTypeSymbol? iasyncDisposableSymbol,
private static void AnalyzeSymbol(
SymbolAnalysisContext context,
INamedTypeSymbol testClassAttributeSymbol,
INamedTypeSymbol idisposableSymbol,
INamedTypeSymbol? iasyncDisposableSymbol,
INamedTypeSymbol? valueTaskSymbol)
{
var methodSymbol = (IMethodSymbol)context.Symbol;

if (methodSymbol.IsAsyncDisposeImplementation(iasyncDisposableSymbol, valueTaskSymbol)
|| methodSymbol.IsDisposeImplementation(idisposableSymbol))
if (methodSymbol.ContainingType.GetAttributes().Any(x => x.AttributeClass.Inherits(testClassAttributeSymbol)) &&
(methodSymbol.IsAsyncDisposeImplementation(iasyncDisposableSymbol, valueTaskSymbol)
|| methodSymbol.IsDisposeImplementation(idisposableSymbol)))
{
context.ReportDiagnostic(methodSymbol.CreateDiagnostic(Rule));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ namespace MSTest.Analyzers.Test;
[TestClass]
public sealed class PreferTestCleanupOverDisposeAnalyzerTests
{
[TestMethod]
public async Task WhenNonTestClassHasDispose_NoDiagnostic()
{
string code = """
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

public class MyNonTestClass : IDisposable
{
public void Dispose()
{
}
}
""";

await VerifyCS.VerifyCodeFixAsync(code, code);
}

[TestMethod]
public async Task WhenTestClassHasDispose_Diagnostic()
{
Expand Down
Loading