Skip to content
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 @@ -59,7 +59,10 @@ public override void Initialize(AnalysisContext context)
var resolvedOperation = WalkDownConversion(returnedValue);
var returnType = resolvedOperation.Type;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is still failing, maybe this will fix it:

Suggested change
var returnType = resolvedOperation.Type;
var returnType = resolvedOperation?.Type;


if (SymbolEqualityComparer.Default.Equals(returnType.OriginalDefinition, wellKnownTypes.TaskOfT))
// Return type could be null if:
// 1. The method returns null.
// 2. The method throws an exception.
if (returnType != null && SymbolEqualityComparer.Default.Equals(returnType.OriginalDefinition, wellKnownTypes.TaskOfT))
{
AddDiagnosticWarning(context, anonymousFunction.Syntax.GetLocation(), returnType);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,42 @@ public class RequestDelegateReturnTypeAnalyzerTests
private string GetMessage(string type) =>
$"The method used to create a RequestDelegate returns Task<{type}>. RequestDelegate discards this value. If this isn't intended then don't return a value or change the method signature to not match RequestDelegate.";

[Fact]
public async Task AnonymousDelegate_RequestDelegate_ThrowError_NoDiagnostics()
{
// Arrange & Act & Assert
await VerifyCS.VerifyAnalyzerAsync(@"
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Builder;
var webApp = WebApplication.Create();
webApp.Use(async (HttpContext context, Func<Task> next) =>
{
context.SetEndpoint(new Endpoint(c => throw new Exception(), EndpointMetadataCollection.Empty, ""Test""));
await next();
});
");
}

[Fact]
public async Task AnonymousDelegate_RequestDelegate_ReturnNull_NoDiagnostics()
{
// Arrange & Act & Assert
await VerifyCS.VerifyAnalyzerAsync(@"
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Builder;
var webApp = WebApplication.Create();
webApp.Use(async (HttpContext context, Func<Task> next) =>
{
context.SetEndpoint(new Endpoint(c => null, EndpointMetadataCollection.Empty, ""Test""));
await next();
});
");
}

[Fact]
public async Task AnonymousDelegate_RequestDelegate_ReturnType_EndpointCtor_ReportDiagnostics()
{
Expand Down