internal void AnalyzeInvocation(SyntaxNodeAnalysisContext context) { if (IsInTaskReturningMethodOrDelegate(context)) { var invocationExpressionSyntax = (InvocationExpressionSyntax)context.Node; var memberAccessSyntax = invocationExpressionSyntax.Expression as MemberAccessExpressionSyntax; if (InspectMemberAccess(context, memberAccessSyntax, CommonInterest.SyncBlockingMethods)) { // Don't return double-diagnostics. return; } MethodDeclarationSyntax invocationDeclaringMethod = invocationExpressionSyntax.FirstAncestorOrSelf <MethodDeclarationSyntax>(); // Also consider all method calls to check for Async-suffixed alternatives. ExpressionSyntax invokedMethodName = CSharpUtils.IsolateMethodName(invocationExpressionSyntax); var symbolInfo = context.SemanticModel.GetSymbolInfo(invocationExpressionSyntax, context.CancellationToken); var methodSymbol = symbolInfo.Symbol as IMethodSymbol; if (methodSymbol != null && !methodSymbol.Name.EndsWith(VSTHRD200UseAsyncNamingConventionAnalyzer.MandatoryAsyncSuffix) && !(methodSymbol.ReturnType?.Name == nameof(Task) && methodSymbol.ReturnType.BelongsToNamespace(Namespaces.SystemThreadingTasks))) { string asyncMethodName = methodSymbol.Name + VSTHRD200UseAsyncNamingConventionAnalyzer.MandatoryAsyncSuffix; var symbols = context.SemanticModel.LookupSymbols( invocationExpressionSyntax.Expression.GetLocation().SourceSpan.Start, methodSymbol.ContainingType, asyncMethodName, includeReducedExtensionMethods: true); foreach (var s in symbols) { if (s is IMethodSymbol m && !m.IsObsolete() && HasSupersetOfParameterTypes(m, methodSymbol) && m.Name != invocationDeclaringMethod?.Identifier.Text && Utils.HasAsyncCompatibleReturnType(m)) { // An async alternative exists. var properties = ImmutableDictionary <string, string> .Empty .Add(AsyncMethodKeyName, asyncMethodName); Diagnostic diagnostic = Diagnostic.Create( Descriptor, invokedMethodName.GetLocation(), properties, invokedMethodName.ToString(), asyncMethodName); context.ReportDiagnostic(diagnostic); return; } } } } }
private void AnalyzeInvocation(SyntaxNodeAnalysisContext context) { var invocationExpressionSyntax = (InvocationExpressionSyntax)context.Node; ExpressionSyntax invokedMethodName = CSharpUtils.IsolateMethodName(invocationExpressionSyntax); var argList = invocationExpressionSyntax.ArgumentList; var symbolInfo = context.SemanticModel.GetSymbolInfo(invocationExpressionSyntax, context.CancellationToken); var methodSymbol = symbolInfo.Symbol as IMethodSymbol; // nameof(X) has no method symbol. So skip over such. if (methodSymbol != null) { var otherOverloads = methodSymbol.ContainingType.GetMembers(methodSymbol.Name).OfType <IMethodSymbol>(); AnalyzeCall(context, invokedMethodName.GetLocation(), argList, methodSymbol, otherOverloads); } }
internal void AnalyzeInvocation(SyntaxNodeAnalysisContext context) { var invocation = (InvocationExpressionSyntax)context.Node; // Only consider invocations that are direct statements. Otherwise, we assume their // result is awaited, assigned, or otherwise consumed. if (invocation.Parent is ExpressionStatementSyntax || invocation.Parent is ConditionalAccessExpressionSyntax) { var methodSymbol = context.SemanticModel.GetSymbolInfo(context.Node).Symbol as IMethodSymbol; if (this.IsAwaitableType(methodSymbol?.ReturnType, context.Compilation, context.CancellationToken)) { if (!CSharpUtils.GetContainingFunction(invocation).IsAsync) { Location?location = (CSharpUtils.IsolateMethodName(invocation) ?? invocation.Expression).GetLocation(); context.ReportDiagnostic(Diagnostic.Create(Descriptor, location)); } } } }
private void AnalyzeInvocation(SyntaxNodeAnalysisContext context) { var invocation = (InvocationExpressionSyntax)context.Node; // Only consider invocations that are direct statements. Otherwise, we assume their // result is awaited, assigned, or otherwise consumed. if (invocation.Parent?.GetType().Equals(typeof(ExpressionStatementSyntax)) ?? false) { var methodSymbol = context.SemanticModel.GetSymbolInfo(context.Node).Symbol as IMethodSymbol; var returnedSymbol = methodSymbol?.ReturnType; if (returnedSymbol?.Name == Types.Task.TypeName && returnedSymbol.BelongsToNamespace(Types.Task.Namespace)) { if (!CSharpUtils.GetContainingFunction(invocation).IsAsync) { var location = (CSharpUtils.IsolateMethodName(invocation) ?? invocation.Expression).GetLocation(); context.ReportDiagnostic(Diagnostic.Create(Descriptor, location)); } } } }