Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix ASP0016 false positive for nested anonymous functions #64174

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

Draft
Copilot wants to merge 2 commits into main
base: main
Choose a base branch
Loading
from copilot/add-test-nested-anonymous-methods
Draft
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
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public override void Initialize(AnalysisContext context)
if (item is IReturnOperation returnOperation &&
returnOperation.ReturnedValue is { } returnedValue)
{
// Skip return operations that belong to nested anonymous functions
if (IsReturnFromNestedAnonymousFunction(returnOperation, anonymousFunction))
{
continue;
}

var resolvedOperation = WalkDownConversion(returnedValue);
var returnType = resolvedOperation.Type;

Expand All @@ -81,6 +87,32 @@ private static void AddDiagnosticWarning(OperationAnalysisContext context, Locat
((INamedTypeSymbol)returnType).TypeArguments[0].ToString()));
}

private static bool IsReturnFromNestedAnonymousFunction(IReturnOperation returnOperation, IAnonymousFunctionOperation targetAnonymousFunction)
{
// Walk up the parent chain from the return operation to see if we encounter
// a nested anonymous function before reaching the target anonymous function
var current = returnOperation.Parent;
while (current != null)
{
if (ReferenceEquals(current, targetAnonymousFunction))
{
// We reached the target anonymous function without finding a nested one
return false;
}

if (current is IAnonymousFunctionOperation)
{
// We found a nested anonymous function before reaching the target
return true;
}

current = current.Parent;
}

// This shouldn't happen in valid code, but return true to be safe
return true;
}

private static IOperation WalkDownConversion(IOperation operation)
{
while (operation is IConversionOperation conversionOperation)
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,42 @@ await VerifyCS.VerifyAnalyzerAsync(@"
webApp.MapGet(""/"", HttpMethod);

static Task HttpMethod(HttpContext context) => Task.CompletedTask;
");
}

[Fact]
public async Task AnonymousDelegate_RequestDelegate_ReturnType_InNestedAnonymousDelegate_DoesNotReportDiagnostics()
{
await VerifyCS.VerifyAnalyzerAsync(@"
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
var webApp = WebApplication.Create();
webApp.Run((context) =>
{
Func<Task<DateTime>> _ = () => Task.FromResult(DateTime.Now);
return Task.CompletedTask;
});
");
}

[Fact]
public async Task AnonymousDelegate_RequestDelegate_ReturnType_InNestedLambdaPassedToMethod_DoesNotReportDiagnostics()
{
// This test simulates the real-world case from the issue where a lambda
// returning Task<Stream> is passed to ThrowsAsync as Func<Task<Stream>>
await VerifyCS.VerifyAnalyzerAsync(@"
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
var webApp = WebApplication.Create();
webApp.Run(async (context) =>
{
await SomeMethodThatTakesFunc(() => Task.FromResult(Stream.Null));
});

static Task SomeMethodThatTakesFunc(Func<Task<Stream>> func) => Task.CompletedTask;
");
}
}
Loading

AltStyle によって変換されたページ (->オリジナル) /