Skip to content

Commit 1a56bdb

Browse files
BArthur1972Bethel Arthur
andauthored
Don't warn on app.UseEndpoints(e => {}); (#48977)
* Update the analyzer to warn only if there are actual invocations in the body of the UseEndpoints delegate. * Move check to separate method and remove diagnostic marker in test source code. --------- Co-authored-by: Bethel Arthur <[email protected]>
1 parent 0e2e6e3 commit 1a56bdb

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

src/Framework/AspNetCoreAnalyzers/src/Analyzers/WebApplicationBuilder/WebApplicationBuilderAnalyzer.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,11 @@ private static bool IsDisallowedMethod(
328328
return false;
329329
}
330330

331+
if (!HasInvocationInBody(disallowedMethodName, invocation))
332+
{
333+
return false;
334+
}
335+
331336
return true;
332337

333338
static bool IsDisallowedMethod(
@@ -353,4 +358,30 @@ static bool IsDisallowedMethod(
353358
return false;
354359
}
355360
}
361+
362+
private static bool HasInvocationInBody(string disallowedMethodName, IInvocationOperation invocation)
363+
{
364+
if (string.Equals(disallowedMethodName, "UseEndpoints", StringComparison.Ordinal))
365+
{
366+
foreach (var argument in invocation.Arguments)
367+
{
368+
var arguments = argument?.Syntax as ArgumentSyntax;
369+
var lambdaExpression = arguments?.Expression as SimpleLambdaExpressionSyntax;
370+
371+
if (lambdaExpression?.Body is BlockSyntax block)
372+
{
373+
foreach (var statement in block.Statements)
374+
{
375+
if (statement is ExpressionStatementSyntax expressionStatement && expressionStatement.Expression is InvocationExpressionSyntax)
376+
{
377+
return true; // Method invocation found, so the diagnostic should be triggered
378+
}
379+
}
380+
return false; // Empty block, no method invocation, so the diagnostic should not be triggered
381+
}
382+
}
383+
}
384+
385+
return true;
386+
}
356387
}

src/Framework/AspNetCoreAnalyzers/test/WebApplicationBuilder/UseTopLevelRouteRegistrationInsteadOfUseEndpointsTest.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public async Task DoesNotWarnWhenEndpointRegistrationIsTopLevel()
3535
}
3636

3737
[Fact]
38-
public async Task DoesNotWarnWhenEnpointRegistrationIsTopLevel_InMain()
38+
public async Task DoesNotWarnWhenEndpointRegistrationIsTopLevel_InMain()
3939
{
4040
//arrange
4141
var source = @"
@@ -58,6 +58,48 @@ public static void Main (string[] args)
5858
Assert.Empty(diagnostics);
5959
}
6060

61+
[Fact]
62+
public async Task DoesNotWarnWhenEndpointRegistrationIsNotTopLevel_NoInvocation()
63+
{
64+
//arrange
65+
var source = TestSource.Read(@"
66+
using Microsoft.AspNetCore.Builder;
67+
var builder = WebApplication.CreateBuilder(args);
68+
var app = builder.Build();
69+
app.UseRouting();
70+
app.UseEndpoints(e => { });
71+
");
72+
//act
73+
var diagnostics = await Runner.GetDiagnosticsAsync(source.Source);
74+
75+
//assert
76+
Assert.Empty(diagnostics);
77+
}
78+
79+
[Fact]
80+
public async Task DoesNotWarnWhenEndpointRegistrationIsNotTopLevel_NoInvocation_InMain()
81+
{
82+
//arrange
83+
var source = TestSource.Read(@"
84+
using Microsoft.AspNetCore.Builder;
85+
public static class Program
86+
{
87+
public static void Main (string[] args)
88+
{
89+
var builder = WebApplication.CreateBuilder(args);
90+
var app = builder.Build();
91+
app.UseRouting();
92+
app.UseEndpoints(e => { });
93+
}
94+
}
95+
");
96+
//act
97+
var diagnostics = await Runner.GetDiagnosticsAsync(source.Source);
98+
99+
//assert
100+
Assert.Empty(diagnostics);
101+
}
102+
61103
[Fact]
62104
public async Task WarnsWhenEndpointRegistrationIsNotTopLevel()
63105
{
@@ -226,4 +268,5 @@ public async Task WarnsTwiceWhenEndpointRegistrationIsNotTopLevel_OnDifferentLin
226268
AnalyzerAssert.DiagnosticLocation(source.MarkerLocations["MM2"], diagnostic2.Location);
227269
Assert.Equal("Suggest using top level route registrations instead of UseEndpoints", diagnostic2.GetMessage(CultureInfo.InvariantCulture));
228270
}
271+
229272
}

0 commit comments

Comments
 (0)