From 392364682bd2717fb59ece0c1175a9b62054e9f3 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Fri, 14 Oct 2022 09:43:53 +0800 Subject: [PATCH] Fix request delegate analyzer error on null return type --- .../Http/RequestDelegateReturnTypeAnalyzer.cs | 5 ++- .../RequestDelegateReturnTypeAnalyzerTests.cs | 36 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/Framework/AspNetCoreAnalyzers/src/Analyzers/Http/RequestDelegateReturnTypeAnalyzer.cs b/src/Framework/AspNetCoreAnalyzers/src/Analyzers/Http/RequestDelegateReturnTypeAnalyzer.cs index 6e5c2132d0e1..93f13b038366 100644 --- a/src/Framework/AspNetCoreAnalyzers/src/Analyzers/Http/RequestDelegateReturnTypeAnalyzer.cs +++ b/src/Framework/AspNetCoreAnalyzers/src/Analyzers/Http/RequestDelegateReturnTypeAnalyzer.cs @@ -59,7 +59,10 @@ public override void Initialize(AnalysisContext context) var resolvedOperation = WalkDownConversion(returnedValue); 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; diff --git a/src/Framework/AspNetCoreAnalyzers/test/Http/RequestDelegateReturnTypeAnalyzerTests.cs b/src/Framework/AspNetCoreAnalyzers/test/Http/RequestDelegateReturnTypeAnalyzerTests.cs index fb8b62e7cb5f..f2c4c344ef4b 100644 --- a/src/Framework/AspNetCoreAnalyzers/test/Http/RequestDelegateReturnTypeAnalyzerTests.cs +++ b/src/Framework/AspNetCoreAnalyzers/test/Http/RequestDelegateReturnTypeAnalyzerTests.cs @@ -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 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 next) => +{ + context.SetEndpoint(new Endpoint(c => null, EndpointMetadataCollection.Empty, ""Test"")); + await next(); +}); +"); + } + [Fact] public async Task AnonymousDelegate_RequestDelegate_ReturnType_EndpointCtor_ReportDiagnostics() {