Repository navigation
Fix ASP0016 false positive for nested anonymous functions - #64174
Merged
Merged
Conversation
Contributor
|
Greetings human! You've submitted a PR that modifies code that is shared with https://github.com/dotnet/runtime . Please make sure you synchronize this code with the changes in that repo! |
Co-authored-by: halter73 <[email protected]>
Copilot
AI
changed the title
[WIP] Add test for nested anonymous methods in RequestDelegateReturnTypeAnalyzerTests
Fix ASP0016 false positive for nested anonymous functions
Oct 27, 2025
halter73
marked this pull request as ready for review
August 7, 2026 00:22
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes an ASP0016 analyzer false positive by ensuring Task<T> returns inside nested anonymous functions within a RequestDelegate body are not treated as returns from the RequestDelegate itself.
Changes:
- Added
IsReturnFromNestedAnonymousFunctionto detect whether anIReturnOperationbelongs to a nested anonymous function (via parent-chain walk). - Updated
RequestDelegateReturnTypeAnalyzerto skip such nested-function returns when evaluatingTask<T>return diagnostics. - Added regression tests covering nested lambdas both as assigned delegates and as lambdas passed to a method parameter.
Show a summary per file
| File | Description |
|---|---|
| src/Framework/AspNetCoreAnalyzers/src/Analyzers/Http/RequestDelegateReturnTypeAnalyzer.cs | Skips IReturnOperations that are part of nested anonymous functions so ASP0016 only analyzes direct RequestDelegate returns. |
| src/Framework/AspNetCoreAnalyzers/test/Http/RequestDelegateReturnTypeAnalyzerTests.cs | Adds regression tests ensuring nested-lambda Task<T> returns don’t trigger ASP0016. |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 1
Youssef1313
reviewed
Aug 7, 2026
Youssef1313
reviewed
Aug 7, 2026
Youssef1313
reviewed
Aug 7, 2026
Youssef1313
reviewed
Aug 7, 2026
Body.Descendants() walks into local function bodies too, but a return
inside a local function has a parent chain of
Block -> ILocalFunctionOperation -> Block -> IAnonymousFunctionOperation
so it never passes through an IAnonymousFunctionOperation. The parent walk
reached the target lambda, and the local function's return was still
treated as the RequestDelegate's own.
Treat ILocalFunctionOperation as a nesting boundary as well, and rename the
helper to IsReturnFromNestedFunction to match. Without this the analyzer
both warns on valid code and, because it bails after the first match, can
report the nested function's return type instead of the RequestDelegate's.
Also flip the unreachable fallback to false so a broken assumption surfaces
as a reported false positive rather than a silently missing diagnostic.
Co-authored-by: Copilot App <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The ASP0016 analyzer incorrectly flagged
Task<T>returns from nested anonymous functions as returns from the parent RequestDelegate. This caused false positives when lambdas returningTask<T>were passed to methods likeAssert.ThrowsAsync:Changes
IsReturnFromNestedAnonymousFunctionhelper that walks the parent chain to detect returns from nested anonymous functionsThe fix uses
ReferenceEqualsto distinguish between the target RequestDelegate and any nested anonymous functions in its body, ensuring only direct returns from the RequestDelegate are analyzed.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.