Skip to content

Handle methods that never return in Api Analyzers #48623

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
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 @@ -74,15 +74,15 @@ void AnalyzeResponseExpression(IReturnOperation returnOperation)

var statementReturnType = returnedValue.Type;

if (!symbolCache.IActionResult.IsAssignableFrom(statementReturnType))
if (statementReturnType is not null && !symbolCache.IActionResult.IsAssignableFrom(statementReturnType))
{
// Return expression is not an instance of IActionResult. Must be returning the "model".
return new ActualApiResponseMetadata(returnOperation, statementReturnType);
}

var defaultStatusCodeAttribute = statementReturnType
.GetAttributes(defaultStatusCodeAttributeSymbol, inherit: true)
.FirstOrDefault();
?.GetAttributes(defaultStatusCodeAttributeSymbol, inherit: true)
?.FirstOrDefault();

// If the type is not annotated with a default status code, then examine
// the attributes on any invoked method returning the type.
Expand Down Expand Up @@ -225,7 +225,7 @@ private static bool TryGetStatusCode(
return false;
}

internal static int? GetDefaultStatusCode(AttributeData attribute)
internal static int? GetDefaultStatusCode(AttributeData? attribute)
{
if (attribute != null &&
attribute.ConstructorArguments.Length == 1 &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public Task NoDiagnosticsAreReturned_ForReturnStatementsInLambdas()
public Task NoDiagnosticsAreReturned_ForReturnStatementsInLocalFunctions()
=> RunNoDiagnosticsAreReturned();

[Fact]
public Task NoDiagnosticsAreReturned_ForApiController_WhenMethodNeverReturns()
=> RunNoDiagnosticsAreReturned();

[Fact]
public async Task DiagnosticsAreReturned_ForIncompleteActionResults()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.AspNetCore.Mvc.Api.Analyzers;

using System;

[ApiController]
[Route("[controller]/[action]")]
public class NoDiagnosticsAreReturned_ForApiController_WhenMethodNeverReturns : ControllerBase
{
public IActionResult GetItem() => throw new NotImplementedException();
}