Skip to content

Commit 312b084

Browse files
Do not trigger analyzer if arguments are open generics (#117)
1 parent 83f8d2d commit 312b084

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/Immediate.Handlers.Analyzers/InvalidIHandlerAnalyzer.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ private void AnalyzeMethod(SymbolAnalysisContext context)
4646
if (!type.IsIHandler())
4747
continue;
4848

49+
if (type.TypeArguments[0].TypeKind is TypeKind.TypeParameter
50+
|| type.TypeArguments[1].TypeKind is TypeKind.TypeParameter)
51+
{
52+
continue;
53+
}
54+
4955
var hasConcrete = context.Compilation
5056
.GetSymbolsWithName("Handler", SymbolFilter.Type, context.CancellationToken)
5157
.Any(h =>
@@ -60,8 +66,8 @@ h is INamedTypeSymbol handler
6066
Diagnostic.Create(
6167
IHandlerMissingImplementation,
6268
parameter.Locations[0],
63-
type.TypeParameters[0].ToDisplayString(),
64-
type.TypeParameters[1].ToDisplayString()
69+
type.TypeArguments[0].ToDisplayString(),
70+
type.TypeArguments[1].ToDisplayString()
6571
)
6672
);
6773
}

tests/Immediate.Handlers.Tests/AnalyzerTests/InvalidIHandlerAnalyzerTests.cs

+27
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,31 @@ private static ValueTask<Response> HandleAsync(
6767
""",
6868
DriverReferenceAssemblies.Normal
6969
).RunAsync();
70+
71+
[Fact]
72+
public async Task AnalyzerDoesNotTriggerForGenericParameter() =>
73+
await AnalyzerTestHelpers.CreateAnalyzerTest<InvalidIHandlerAnalyzer>(
74+
"""
75+
using System;
76+
using System.Collections.Generic;
77+
using System.IO;
78+
using System.Linq;
79+
using System.Net.Http;
80+
using System.Threading;
81+
using System.Threading.Tasks;
82+
using Immediate.Handlers.Shared;
83+
84+
public static class Test<TRequest>
85+
{
86+
public static void Method(IHandler<TRequest, int> handler)
87+
{
88+
}
89+
90+
public static void Method<TResponse>(IHandler<int, TResponse> handler)
91+
{
92+
}
93+
}
94+
""",
95+
DriverReferenceAssemblies.Normal
96+
).RunAsync();
7097
}

0 commit comments

Comments
 (0)