|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections.Immutable; |
| 5 | +using Microsoft.CodeAnalysis; |
| 6 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 7 | +using Microsoft.CodeAnalysis.Operations; |
| 8 | + |
| 9 | +namespace Microsoft.AspNetCore.Analyzers.Http; |
| 10 | + |
| 11 | +[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 12 | +public partial class RequestDelegateReturnTypeAnalyzer : DiagnosticAnalyzer |
| 13 | +{ |
| 14 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(DiagnosticDescriptors.DoNotReturnValueFromRequestDelegate); |
| 15 | + |
| 16 | + public override void Initialize(AnalysisContext context) |
| 17 | + { |
| 18 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 19 | + context.EnableConcurrentExecution(); |
| 20 | + context.RegisterCompilationStartAction(context => |
| 21 | + { |
| 22 | + var compilation = context.Compilation; |
| 23 | + |
| 24 | + if (!WellKnownTypes.TryCreate(compilation, out var wellKnownTypes)) |
| 25 | + { |
| 26 | + return; |
| 27 | + } |
| 28 | + context.RegisterOperationAction(context => |
| 29 | + { |
| 30 | + var methodReference = (IMethodReferenceOperation)context.Operation; |
| 31 | + if (methodReference.Parent is { } parent && |
| 32 | + parent.Kind == OperationKind.DelegateCreation && |
| 33 | + SymbolEqualityComparer.Default.Equals(parent.Type, wellKnownTypes.RequestDelegate)) |
| 34 | + { |
| 35 | + // Inspect return type of method signature for Task<T>. |
| 36 | + var returnType = methodReference.Method.ReturnType; |
| 37 | + |
| 38 | + if (SymbolEqualityComparer.Default.Equals(returnType.OriginalDefinition, wellKnownTypes.TaskOfT)) |
| 39 | + { |
| 40 | + AddDiagnosticWarning(context, methodReference.Syntax.GetLocation(), returnType); |
| 41 | + } |
| 42 | + } |
| 43 | + }, OperationKind.MethodReference); |
| 44 | + context.RegisterOperationAction(context => |
| 45 | + { |
| 46 | + var anonymousFunction = (IAnonymousFunctionOperation)context.Operation; |
| 47 | + if (anonymousFunction.Parent is { } parent && |
| 48 | + parent.Kind == OperationKind.DelegateCreation && |
| 49 | + SymbolEqualityComparer.Default.Equals(parent.Type, wellKnownTypes.RequestDelegate)) |
| 50 | + { |
| 51 | + // Inspect contents of anonymous function and search for return statements. |
| 52 | + // Return statement of Task<T> means a value was returned. |
| 53 | + foreach (var item in anonymousFunction.Body.Descendants()) |
| 54 | + { |
| 55 | + if (item.Kind == OperationKind.Return && |
| 56 | + item is IReturnOperation returnOperation && |
| 57 | + returnOperation.ReturnedValue is { } returnedValue) |
| 58 | + { |
| 59 | + var resolvedOperation = WalkDownConversion(returnedValue); |
| 60 | + var returnType = resolvedOperation.Type; |
| 61 | + |
| 62 | + if (SymbolEqualityComparer.Default.Equals(returnType.OriginalDefinition, wellKnownTypes.TaskOfT)) |
| 63 | + { |
| 64 | + AddDiagnosticWarning(context, anonymousFunction.Syntax.GetLocation(), returnType); |
| 65 | + return; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + }, OperationKind.AnonymousFunction); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + private static void AddDiagnosticWarning(OperationAnalysisContext context, Location location, ITypeSymbol returnType) |
| 75 | + { |
| 76 | + context.ReportDiagnostic(Diagnostic.Create( |
| 77 | + DiagnosticDescriptors.DoNotReturnValueFromRequestDelegate, |
| 78 | + location, |
| 79 | + ((INamedTypeSymbol)returnType).TypeArguments[0].ToString())); |
| 80 | + } |
| 81 | + |
| 82 | + private static IOperation WalkDownConversion(IOperation operation) |
| 83 | + { |
| 84 | + while (operation is IConversionOperation conversionOperation) |
| 85 | + { |
| 86 | + operation = conversionOperation.Operand; |
| 87 | + } |
| 88 | + |
| 89 | + return operation; |
| 90 | + } |
| 91 | +} |
0 commit comments