Esempio n. 1
0
        private void AnalyzeAwaitExpression(SyntaxNodeAnalysisContext context)
        {
            var invocation = (AwaitExpressionSyntax)context.Node;

            var configureAwaitConfig = ConfigureAwaitConfiguration.TryGetConfigureAwait(context.Compilation);

            if (configureAwaitConfig == ConfigureAwait.UseConfigureAwaitFalse)
            {
                var operation = context.SemanticModel.GetOperation(invocation, context.CancellationToken);
                if (operation is IAwaitOperation awaitOperation)
                {
                    if (awaitOperation.Operation is IInvocationOperation configureAwaitOperation)
                    {
                        if (configureAwaitOperation.TargetMethod.IsConfigureAwait(context.Compilation))
                        {
                            return;
                        }

                        if (configureAwaitOperation.Type.IsClrType(context.Compilation, typeof(YieldAwaitable)))
                        {
                            return;
                        }
                    }

                    var location = awaitOperation.Syntax.GetLocation();

                    var diagnostic = Diagnostic.Create(Rule, location);
                    context.ReportDiagnostic(diagnostic);
                }
            }
        }
        private void AnalyzeAwaitExpression(SyntaxNodeAnalysisContext context)
        {
            var invocation = (AwaitExpressionSyntax)context.Node;

            var configureAwaitConfig = ConfigureAwaitConfiguration.TryGetConfigureAwait(context.Compilation);

            if (configureAwaitConfig == ConfigureAwait.DoNotUseConfigureAwait)
            {
                var operation = context.SemanticModel.GetOperation(invocation, context.CancellationToken);
                if (operation is IAwaitOperation awaitOperation &&
                    awaitOperation.Operation is IInvocationOperation configureAwaitOperation &&
                    configureAwaitOperation.TargetMethod.IsConfigureAwait(context.Compilation))
                {
                    if (configureAwaitOperation.Arguments.Length != 0 &&
                        configureAwaitOperation.Arguments[0].Value is ILiteralOperation literal &&
                        literal.ConstantValue.HasValue &&
                        literal.ConstantValue.Value.Equals(false))
                    {
                        var location = configureAwaitOperation.Syntax.GetLocation();

                        // Need to find 'ConfigureAwait' node.
                        if (configureAwaitOperation.Syntax is InvocationExpressionSyntax i &&
                            i.Expression is
                            MemberAccessExpressionSyntax mae)
                        {
                            // This is a really weird way for getting a location for 'ConfigureAwait(false)' span!

                            var argsLocation = i.ArgumentList.GetLocation();
                            var nameLocation = mae.Name.GetLocation().SourceSpan;
                            location = Location.Create(argsLocation.SourceTree,
                                                       TextSpan.FromBounds(nameLocation.Start, argsLocation.SourceSpan.End));
                        }

                        var diagnostic = Diagnostic.Create(UnnecessaryWithSuggestionDescriptor, location);
                        context.ReportDiagnostic(diagnostic);
                    }
                }
            }
        }