Skip to content

Commit a98400b

Browse files
committed
Improve detection of DDI-provided code
Since we skip that for determining if the user invoked AddServices.
1 parent 31b504b commit a98400b

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/DependencyInjection/AddServicesAnalyzer.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ public override void Initialize(AnalysisContext context)
4040

4141
Location? location = default;
4242

43+
static bool IsDDICode(SyntaxNode node, SemanticModel semantic)
44+
{
45+
if (node.Ancestors().OfType<MethodDeclarationSyntax>().FirstOrDefault() is { } method &&
46+
semantic.GetDeclaredSymbol(method) is { } declaration &&
47+
declaration.GetAttributes().Any(attr => attr.AttributeClass?.Name == "DDIAddServicesAttribute"))
48+
return true;
49+
50+
return false;
51+
}
52+
4353
startContext.RegisterSemanticModelAction(semanticContext =>
4454
{
4555
var semantic = semanticContext.SemanticModel;
@@ -48,9 +58,8 @@ public override void Initialize(AnalysisContext context)
4858
.DescendantNodes()
4959
.OfType<InvocationExpressionSyntax>()
5060
.Select(invocation => new { Invocation = invocation, semantic.GetSymbolInfo(invocation, semanticContext.CancellationToken).Symbol })
51-
// We don't consider invocations from methods that have the DDIAddServicesAttribute as user-provided, since we do that
52-
// in our type/regex overloads. Users need to invoke those methods in turn.
53-
.Where(x => x.Symbol is IMethodSymbol method && !method.GetAttributes().Any(attr => attr.AttributeClass?.Name == "DDIAddServicesAttribute"))
61+
// It has to be user-provided code, not our own extensions/overloads.
62+
.Where(x => !IsDDICode(x.Invocation, semantic))
5463
.Select(x => new { x.Invocation, Method = (IMethodSymbol)x.Symbol! });
5564

5665
bool IsServiceCollectionExtension(IMethodSymbol method) => method.IsExtensionMethod &&

0 commit comments

Comments
 (0)