Ejemplo n.º 1
0
        protected virtual Diagnostic AnalyzeExpression(ExpressionSyntax expression, SemanticModel semanticModel)
        {
            var variableNameExtractor = new VariableNameExtractor(semanticModel);

            expression.Accept(variableNameExtractor);

            var typeInfo = semanticModel.GetTypeInfo(variableNameExtractor.VariableIdentifierName);

            if (typeInfo.ConvertedType == null)
            {
                return(null);
            }
            if (!ShouldAnalyzeVariableType(typeInfo.ConvertedType))
            {
                return(null);
            }

            foreach (var visitor in Visitors)
            {
                expression.Accept(visitor);

                if (visitor.IsValid(expression))
                {
                    return(CreateDiagnostic(visitor, expression));
                }
            }
            return(null);
        }
        public static string ExtractVariabeName(InvocationExpressionSyntax invocation)
        {
            var variableExtractor = new VariableNameExtractor();

            invocation.Accept(variableExtractor);

            return(variableExtractor.VariableName);
        }
Ejemplo n.º 3
0
        public static bool ArgumentIsVariablePredicate(ArgumentSyntax argument)
        {
            if (argument.Expression is IdentifierNameSyntax identifier)
            {
                var argumentName = identifier.Identifier.Text;

                var variableName = VariableNameExtractor.ExtractVariabeName(argument);

                return(variableName == argumentName);
            }
            return(false);
        }
            private static bool ArgumentInvokesDistinctMethod(SeparatedSyntaxList <ArgumentSyntax> arguments)
            {
                if (!arguments.Any())
                {
                    return(false);
                }

                if (arguments.First().Expression is InvocationExpressionSyntax invocation)
                {
                    var visitor = new FluentAssertionsCSharpSyntaxVisitor(new MemberValidator(nameof(Enumerable.Distinct)));
                    invocation.Accept(visitor);

                    return(visitor.IsValid(invocation) && VariableNameExtractor.ExtractVariabeName(arguments.First()) == VariableNameExtractor.ExtractVariabeName(invocation));
                }
                return(false);
            }