Skip to content

Don't warn on app.UseEndpoints(e => {}); #48977

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

Merged
merged 3 commits into from
Jun 23, 2023
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 @@ -328,6 +328,11 @@ private static bool IsDisallowedMethod(
return false;
}

if (!HasInvocationInBody(disallowedMethodName, invocation))
{
return false;
}

return true;

static bool IsDisallowedMethod(
Expand All @@ -353,4 +358,30 @@ static bool IsDisallowedMethod(
return false;
}
}

private static bool HasInvocationInBody(string disallowedMethodName, IInvocationOperation invocation)
{
if (string.Equals(disallowedMethodName, "UseEndpoints", StringComparison.Ordinal))
{
foreach (var argument in invocation.Arguments)
{
var arguments = argument?.Syntax as ArgumentSyntax;
var lambdaExpression = arguments?.Expression as SimpleLambdaExpressionSyntax;

if (lambdaExpression?.Body is BlockSyntax block)
{
foreach (var statement in block.Statements)
{
if (statement is ExpressionStatementSyntax expressionStatement && expressionStatement.Expression is InvocationExpressionSyntax)
{
return true; // Method invocation found, so the diagnostic should be triggered
}
}
return false; // Empty block, no method invocation, so the diagnostic should not be triggered
}
}
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public async Task DoesNotWarnWhenEndpointRegistrationIsTopLevel()
}

[Fact]
public async Task DoesNotWarnWhenEnpointRegistrationIsTopLevel_InMain()
public async Task DoesNotWarnWhenEndpointRegistrationIsTopLevel_InMain()
{
//arrange
var source = @"
Expand All @@ -58,6 +58,48 @@ public static void Main (string[] args)
Assert.Empty(diagnostics);
}

[Fact]
public async Task DoesNotWarnWhenEndpointRegistrationIsNotTopLevel_NoInvocation()
{
//arrange
var source = TestSource.Read(@"
using Microsoft.AspNetCore.Builder;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.UseRouting();
app.UseEndpoints(e => { });
");
//act
var diagnostics = await Runner.GetDiagnosticsAsync(source.Source);

//assert
Assert.Empty(diagnostics);
}

[Fact]
public async Task DoesNotWarnWhenEndpointRegistrationIsNotTopLevel_NoInvocation_InMain()
{
//arrange
var source = TestSource.Read(@"
using Microsoft.AspNetCore.Builder;
public static class Program
{
public static void Main (string[] args)
{
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.UseRouting();
app.UseEndpoints(e => { });
}
}
");
//act
var diagnostics = await Runner.GetDiagnosticsAsync(source.Source);

//assert
Assert.Empty(diagnostics);
}

[Fact]
public async Task WarnsWhenEndpointRegistrationIsNotTopLevel()
{
Expand Down Expand Up @@ -226,4 +268,5 @@ public async Task WarnsTwiceWhenEndpointRegistrationIsNotTopLevel_OnDifferentLin
AnalyzerAssert.DiagnosticLocation(source.MarkerLocations["MM2"], diagnostic2.Location);
Assert.Equal("Suggest using top level route registrations instead of UseEndpoints", diagnostic2.GetMessage(CultureInfo.InvariantCulture));
}

}