Skip to content

Commit

Permalink
Do not trigger analyzer if arguments are open generics
Browse files Browse the repository at this point in the history
  • Loading branch information
viceroypenguin committed Sep 10, 2024
1 parent 83f8d2d commit e5d6b72
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Immediate.Handlers.Analyzers/InvalidIHandlerAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ private void AnalyzeMethod(SymbolAnalysisContext context)
if (!type.IsIHandler())
continue;

if (type.TypeArguments[0].TypeKind is TypeKind.TypeParameter
|| type.TypeArguments[1].TypeKind is TypeKind.TypeParameter)
{
continue;
}

var hasConcrete = context.Compilation
.GetSymbolsWithName("Handler", SymbolFilter.Type, context.CancellationToken)
.Any(h =>
Expand All @@ -60,8 +66,8 @@ h is INamedTypeSymbol handler
Diagnostic.Create(
IHandlerMissingImplementation,
parameter.Locations[0],
type.TypeParameters[0].ToDisplayString(),
type.TypeParameters[1].ToDisplayString()
type.TypeArguments[0].ToDisplayString(),
type.TypeArguments[1].ToDisplayString()
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,31 @@ private static ValueTask<Response> HandleAsync(
""",
DriverReferenceAssemblies.Normal
).RunAsync();

[Fact]
public async Task AnalyzerDoesNotTriggerForGenericParameter() =>
await AnalyzerTestHelpers.CreateAnalyzerTest<InvalidIHandlerAnalyzer>(
"""
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Immediate.Handlers.Shared;
public static class Test<TRequest>
{
public static void Method(IHandler<TRequest, int> handler)
{
}
public static void Method<TResponse>(IHandler<int, TResponse> handler)
{
}
}
""",
DriverReferenceAssemblies.Normal
).RunAsync();
}

0 comments on commit e5d6b72

Please sign in to comment.