Exemplo n.º 1
0
 private static bool IsSumOnInteger(IMethodSymbol methodSymbol, SemanticModel semanticModel)
 {
     return(methodSymbol != null &&
            CollectionEmptinessChecking.MethodIsOnIEnumerable(methodSymbol, semanticModel) &&
            methodSymbol.Name == "Sum" &&
            IsReturnTypeCandidate(methodSymbol));
 }
Exemplo n.º 2
0
        private static void CheckCountCall(SyntaxNodeAnalysisContext c)
        {
            var invocation   = (InvocationExpressionSyntax)c.Node;
            var methodSymbol = c.SemanticModel.GetSymbolInfo(invocation).Symbol as IMethodSymbol;

            if (invocation.ArgumentList == null ||
                invocation.ArgumentList.Arguments.Any() ||
                methodSymbol == null ||
                !CollectionEmptinessChecking.MethodIsOnIEnumerable(methodSymbol, c.SemanticModel) ||
                methodSymbol.Name != "Count")
            {
                return;
            }

            var memberAccess = invocation.Expression as MemberAccessExpressionSyntax;

            if (memberAccess == null)
            {
                return;
            }

            var symbol = c.SemanticModel.GetTypeInfo(memberAccess.Expression).Type;

            if (symbol.GetMembers("Count").OfType <IPropertySymbol>().Any())
            {
                c.ReportDiagnostic(Diagnostic.Create(Rule, GetReportLocation(invocation),
                                                     string.Format(MessageUseInstead, "\"Count\" property")));
            }
        }
Exemplo n.º 3
0
        private static bool IsMethodOrderByExtension(InvocationExpressionSyntax invocation, SemanticModel semanticModel)
        {
            if (invocation == null)
            {
                return(false);
            }

            var methodSymbol = semanticModel.GetSymbolInfo(invocation).Symbol as IMethodSymbol;

            return(methodSymbol != null &&
                   methodSymbol.Name == "OrderBy" &&
                   methodSymbol.MethodKind == MethodKind.ReducedExtension &&
                   CollectionEmptinessChecking.MethodIsOnIEnumerable(methodSymbol, semanticModel));
        }
Exemplo n.º 4
0
        private static void CheckExtensionMethodsOnIEnumerable(SyntaxNodeAnalysisContext c)
        {
            var outerInvocation   = (InvocationExpressionSyntax)c.Node;
            var outerMethodSymbol = c.SemanticModel.GetSymbolInfo(outerInvocation).Symbol as IMethodSymbol;

            if (outerMethodSymbol == null ||
                !CollectionEmptinessChecking.MethodIsOnIEnumerable(outerMethodSymbol, c.SemanticModel))
            {
                return;
            }

            var innerInvocation = GetInnerInvocation(outerInvocation, outerMethodSymbol);

            if (innerInvocation == null)
            {
                return;
            }

            var innerMethodSymbol = c.SemanticModel.GetSymbolInfo(innerInvocation).Symbol as IMethodSymbol;

            if (innerMethodSymbol == null ||
                !CollectionEmptinessChecking.MethodIsOnIEnumerable(innerMethodSymbol, c.SemanticModel))
            {
                return;
            }

            if (CheckForSimplifiable(outerMethodSymbol, outerInvocation, innerMethodSymbol, innerInvocation, c))
            {
                return;
            }

            if (CheckForCastSimplification(outerMethodSymbol, innerMethodSymbol, innerInvocation,
                                           outerInvocation, c))
            {
                return;
            }
        }
        public override void Initialize(AnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var outerInvocation   = (InvocationExpressionSyntax)c.Node;
                var outerMethodSymbol = c.SemanticModel.GetSymbolInfo(outerInvocation).Symbol as IMethodSymbol;
                if (outerMethodSymbol == null ||
                    !CollectionEmptinessChecking.MethodIsOnIEnumerable(outerMethodSymbol, c.SemanticModel))
                {
                    return;
                }

                InvocationExpressionSyntax innerInvocation;
                if (outerMethodSymbol.MethodKind == MethodKind.ReducedExtension)
                {
                    var memberAccess = outerInvocation.Expression as MemberAccessExpressionSyntax;
                    if (memberAccess == null)
                    {
                        return;
                    }
                    innerInvocation = memberAccess.Expression as InvocationExpressionSyntax;
                }
                else
                {
                    var argument = outerInvocation.ArgumentList.Arguments.FirstOrDefault();
                    if (argument == null)
                    {
                        return;
                    }
                    innerInvocation = argument.Expression as InvocationExpressionSyntax;
                }

                if (innerInvocation == null)
                {
                    return;
                }

                var innerMethodSymbol = c.SemanticModel.GetSymbolInfo(innerInvocation).Symbol as IMethodSymbol;
                if (innerMethodSymbol == null ||
                    !CollectionEmptinessChecking.MethodIsOnIEnumerable(innerMethodSymbol, c.SemanticModel))
                {
                    return;
                }

                var outerArguments = GetReducedArguments(outerMethodSymbol, outerInvocation);

                if (CheckForSimplifiable(outerArguments, outerMethodSymbol, innerMethodSymbol, c,
                                         innerInvocation))
                {
                    return;
                }

                if (CheckForCastSimplification(outerMethodSymbol, innerMethodSymbol, innerInvocation,
                                               outerInvocation, c))
                {
                    return;
                }
            },
                SyntaxKind.InvocationExpression);
        }