private static bool IsInvocationWithExplicitArray(ArgumentListSyntax argumentList, IMethodSymbol invokedMethodSymbol,
                                                          SemanticModel semanticModel)
        {
            var allParameterMatches = new List <IParameterSymbol>();

            foreach (var argument in argumentList.Arguments)
            {
                var parameter = MethodParameterLookup.GetParameterSymbol(argument, argumentList, invokedMethodSymbol);
                allParameterMatches.Add(parameter);
                if (parameter.IsParams)
                {
                    var argType = semanticModel.GetTypeInfo(argument.Expression).Type;
                    if (!(argType is IArrayTypeSymbol))
                    {
                        return(false);
                    }
                }
            }

            return(allParameterMatches.Count(p => p.IsParams) == 1);
        }
        private static bool ArgumentsMatchParameters(ArgumentListSyntax argumentList, List <INamedTypeSymbol> argumentTypes,
                                                     IMethodSymbol possibleOtherMethod, SemanticModel semanticModel)
        {
            var matchedParameters = new List <IParameterSymbol>();

            for (int i = 0; i < argumentList.Arguments.Count; i++)
            {
                var argument     = argumentList.Arguments[i];
                var argumentType = argumentTypes[i];
                var parameter    = MethodParameterLookup.GetParameterSymbol(argument, argumentList, possibleOtherMethod);
                if (parameter == null)
                {
                    return(false);
                }

                if (argumentType == null)
                {
                    if (!parameter.Type.IsReferenceType)
                    {
                        return(false);
                    }
                }
                else
                {
                    var conversion = semanticModel.ClassifyConversion(argument.Expression, parameter.Type);
                    if (!conversion.IsImplicit)
                    {
                        return(false);
                    }
                }

                matchedParameters.Add(parameter);
            }

            return(!possibleOtherMethod.Parameters.Except(matchedParameters).Any(p => !p.HasExplicitDefaultValue));
        }
Exemple #3
0
        public override void Initialize(AnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var assignment  = (AssignmentExpressionSyntax)c.Node;
                var typeDerived = c.SemanticModel.GetTypeInfo(assignment.Right).Type;
                var typeBase    = c.SemanticModel.GetTypeInfo(assignment.Left).Type;

                if (AreCovariantArrayTypes(typeDerived, typeBase))
                {
                    c.ReportDiagnostic(Diagnostic.Create(Rule, assignment.Right.GetLocation()));
                }
            },
                SyntaxKind.SimpleAssignmentExpression);

            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var variableDeclaration = (VariableDeclarationSyntax)c.Node;
                var typeBase            = c.SemanticModel.GetTypeInfo(variableDeclaration.Type).Type;

                foreach (var variable in variableDeclaration.Variables
                         .Where(syntax => syntax.Initializer != null))
                {
                    var typeDerived = c.SemanticModel.GetTypeInfo(variable.Initializer.Value).Type;

                    if (AreCovariantArrayTypes(typeDerived, typeBase))
                    {
                        c.ReportDiagnostic(Diagnostic.Create(Rule, variable.Initializer.Value.GetLocation()));
                    }
                }
            },
                SyntaxKind.VariableDeclaration);

            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var invocation            = (InvocationExpressionSyntax)c.Node;
                var methodParameterLookup = new MethodParameterLookup(invocation, c.SemanticModel);

                foreach (var argument in invocation.ArgumentList.Arguments)
                {
                    var parameter = methodParameterLookup.GetParameterSymbol(argument);
                    if (parameter == null)
                    {
                        continue;
                    }

                    if (parameter.IsParams)
                    {
                        continue;
                    }

                    var typeDerived = c.SemanticModel.GetTypeInfo(argument.Expression).Type;
                    if (AreCovariantArrayTypes(typeDerived, parameter.Type))
                    {
                        c.ReportDiagnostic(Diagnostic.Create(Rule, argument.GetLocation()));
                    }
                }
            },
                SyntaxKind.InvocationExpression);

            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var castExpression = (CastExpressionSyntax)c.Node;
                var typeDerived    = c.SemanticModel.GetTypeInfo(castExpression.Expression).Type;
                var typeBase       = c.SemanticModel.GetTypeInfo(castExpression.Type).Type;

                if (AreCovariantArrayTypes(typeDerived, typeBase))
                {
                    c.ReportDiagnostic(Diagnostic.Create(Rule, castExpression.Type.GetLocation()));
                }
            },
                SyntaxKind.CastExpression);
        }
Exemple #4
0
        public override void Initialize(AnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var methodCall            = (InvocationExpressionSyntax)c.Node;
                var methodParameterLookup = new MethodParameterLookup(methodCall, c.SemanticModel);
                var argumentMappings      = methodCall.ArgumentList.Arguments.Select(argument =>
                                                                                     new KeyValuePair <ArgumentSyntax, IParameterSymbol>(argument,
                                                                                                                                         methodParameterLookup.GetParameterSymbol(argument)))
                                            .ToList();

                var methodSymbol = methodParameterLookup.MethodSymbol;
                if (methodSymbol == null)
                {
                    return;
                }

                foreach (var argumentMapping in argumentMappings)
                {
                    if (ParameterHasCallerInfoAttribute(argumentMapping))
                    {
                        var argument = argumentMapping.Key;
                        c.ReportDiagnostic(Diagnostic.Create(Rule, argument.GetLocation()));
                    }
                }
            },
                SyntaxKind.InvocationExpression);
        }
Exemple #5
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic = context.Diagnostics.First();
            var invocation = GetInvocation(root, diagnostic.Location.SourceSpan);

            if (invocation == null)
            {
                return;
            }

            var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken);

            var methodParameterLookup = new MethodParameterLookup(invocation, semanticModel);
            var argumentMappings      = invocation.ArgumentList.Arguments
                                        .Select(argument =>
                                                new RedundantArgument.ArgumentParameterMapping(argument,
                                                                                               methodParameterLookup.GetParameterSymbol(argument)));

            var methodSymbol = methodParameterLookup.MethodSymbol;

            if (methodSymbol == null)
            {
                return;
            }

            var argumentsWithDefaultValues        = new List <ArgumentSyntax>();
            var argumentsCanBeRemovedWithoutNamed = new List <ArgumentSyntax>();
            var canBeRemovedWithoutNamed          = true;

            var reversedMappings = argumentMappings.Reverse();

            foreach (var argumentMapping in reversedMappings)
            {
                var argument = argumentMapping.Argument;

                if (RedundantArgument.ArgumentHasDefaultValue(argumentMapping, semanticModel))
                {
                    argumentsWithDefaultValues.Add(argument);

                    if (canBeRemovedWithoutNamed)
                    {
                        argumentsCanBeRemovedWithoutNamed.Add(argument);
                    }
                }
                else
                {
                    if (argument.NameColon == null)
                    {
                        canBeRemovedWithoutNamed = false;
                    }
                }
            }

            if (argumentsCanBeRemovedWithoutNamed.Any())
            {
                context.RegisterCodeFix(
                    CodeAction.Create(
                        TitleRemove,
                        c => RemoveArgumentsAsync(context.Document, argumentsCanBeRemovedWithoutNamed, c),
                        TitleRemove),
                    context.Diagnostics);
            }

            var cannotBeRemoved = argumentsWithDefaultValues.Except(argumentsCanBeRemovedWithoutNamed);

            if (cannotBeRemoved.Any())
            {
                context.RegisterCodeFix(
                    CodeAction.Create(
                        TitleRemoveWithNameAdditions,
                        c => RemoveArgumentsAndAddNecessaryNamesAsync(context.Document, invocation.ArgumentList,
                                                                      argumentMappings, argumentsWithDefaultValues, semanticModel, c),
                        TitleRemoveWithNameAdditions),
                    context.Diagnostics);
            }
        }
Exemple #6
0
        public override void Initialize(AnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var methodCall                = (InvocationExpressionSyntax)c.Node;
                var methodParameterLookup     = new MethodParameterLookup(methodCall, c.SemanticModel);
                var argumentParameterMappings = methodCall.ArgumentList.Arguments.Select(argument =>
                                                                                         new KeyValuePair <ArgumentSyntax, IParameterSymbol>(argument,
                                                                                                                                             methodParameterLookup.GetParameterSymbol(argument)))
                                                .ToDictionary(pair => pair.Key, pair => pair.Value);

                var methodSymbol = methodParameterLookup.MethodSymbol;

                if (methodSymbol == null)
                {
                    return;
                }

                var parameterNames = argumentParameterMappings.Values
                                     .Select(symbol => symbol.Name)
                                     .Distinct()
                                     .ToList();

                var identifierArguments = GetIdentifierArguments(methodCall);
                var identifierNames     = identifierArguments
                                          .Select(p => p.IdentifierName)
                                          .ToList();

                if (!parameterNames.Intersect(identifierNames).Any())
                {
                    return;
                }

                var methodCallHasIssue = false;

                for (var i = 0; !methodCallHasIssue && i < identifierArguments.Count; i++)
                {
                    var identifierArgument = identifierArguments[i];
                    var identifierName     = identifierArgument.IdentifierName;
                    var parameter          = argumentParameterMappings[identifierArgument.ArgumentSyntax];
                    var parameterName      = parameter.Name;

                    if (string.IsNullOrEmpty(identifierName) ||
                        !parameterNames.Contains(identifierName))
                    {
                        continue;
                    }

                    var positional = identifierArgument as PositionalIdentifierArgument;
                    if (positional != null &&
                        (parameter.IsParams ||
                         !identifierNames.Contains(parameterName) ||
                         identifierName == parameterName))
                    {
                        continue;
                    }

                    var named = identifierArgument as NamedIdentifierArgument;
                    if (named != null &&
                        (!identifierNames.Contains(named.DeclaredName) || named.DeclaredName == named.IdentifierName))
                    {
                        continue;
                    }

                    methodCallHasIssue = true;
                }

                if (methodCallHasIssue)
                {
                    var memberAccess   = methodCall.Expression as MemberAccessExpressionSyntax;
                    var reportLocation = memberAccess == null
                            ? methodCall.Expression.GetLocation()
                            : memberAccess.Name.GetLocation();

                    c.ReportDiagnostic(Diagnostic.Create(Rule, reportLocation,
                                                         methodSymbol.Name));
                }
            },
                SyntaxKind.InvocationExpression);
        }
        public override void Initialize(AnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var methodCall            = (InvocationExpressionSyntax)c.Node;
                var methodParameterLookup = new MethodParameterLookup(methodCall, c.SemanticModel);
                var argumentMappings      = methodCall.ArgumentList.Arguments.Select(argument =>
                                                                                     new ArgumentParameterMapping(argument,
                                                                                                                  methodParameterLookup.GetParameterSymbol(argument)))
                                            .ToList();

                var methodSymbol = methodParameterLookup.MethodSymbol;
                if (methodSymbol == null)
                {
                    return;
                }

                foreach (var argumentMapping in argumentMappings)
                {
                    if (ArgumentHasDefaultValue(argumentMapping, c.SemanticModel))
                    {
                        var argument  = argumentMapping.Argument;
                        var parameter = argumentMapping.Parameter;
                        c.ReportDiagnostic(Diagnostic.Create(Rule, argument.GetLocation(), parameter.Name));
                    }
                }
            },
                SyntaxKind.InvocationExpression);
        }
        public override void Initialize(AnalysisContext context)
        {
            context.RegisterSyntaxNodeAction(
                c =>
                {
                    var assignment = (AssignmentExpressionSyntax) c.Node;
                    var typeDerived = c.SemanticModel.GetTypeInfo(assignment.Right).Type;
                    var typeBase = c.SemanticModel.GetTypeInfo(assignment.Left).Type;

                    if (AreCovariantArrayTypes(typeDerived, typeBase))
                    {
                        c.ReportDiagnostic(Diagnostic.Create(Rule, assignment.Right.GetLocation()));
                    }
                },
                SyntaxKind.SimpleAssignmentExpression);

            context.RegisterSyntaxNodeAction(
                c =>
                {
                    var variableDeclaration = (VariableDeclarationSyntax)c.Node;
                    var typeBase = c.SemanticModel.GetTypeInfo(variableDeclaration.Type).Type;

                    foreach (var variable in variableDeclaration.Variables
                        .Where(syntax => syntax.Initializer != null))
                    {
                        var typeDerived = c.SemanticModel.GetTypeInfo(variable.Initializer.Value).Type;

                        if (AreCovariantArrayTypes(typeDerived, typeBase))
                        {
                            c.ReportDiagnostic(Diagnostic.Create(Rule, variable.Initializer.Value.GetLocation()));
                        }
                    }
                },
                SyntaxKind.VariableDeclaration);

            context.RegisterSyntaxNodeAction(
                c =>
                {
                    var invocation = (InvocationExpressionSyntax)c.Node;
                    var methodParameterLookup = new MethodParameterLookup(invocation, c.SemanticModel);

                    foreach (var argument in invocation.ArgumentList.Arguments)
                    {
                        var parameter = methodParameterLookup.GetParameterSymbol(argument);
                        if (parameter == null)
                        {
                            continue;
                        }

                        if (parameter.IsParams)
                        {
                            continue;
                        }

                        var typeDerived = c.SemanticModel.GetTypeInfo(argument.Expression).Type;
                        if (AreCovariantArrayTypes(typeDerived, parameter.Type))
                        {
                            c.ReportDiagnostic(Diagnostic.Create(Rule, argument.GetLocation()));
                        }
                    }
                },
                SyntaxKind.InvocationExpression);

            context.RegisterSyntaxNodeAction(
                c =>
                {
                    var castExpression = (CastExpressionSyntax) c.Node;
                    var typeDerived = c.SemanticModel.GetTypeInfo(castExpression.Expression).Type;
                    var typeBase = c.SemanticModel.GetTypeInfo(castExpression.Type).Type;

                    if (AreCovariantArrayTypes(typeDerived, typeBase))
                    {
                        c.ReportDiagnostic(Diagnostic.Create(Rule, castExpression.Type.GetLocation()));
                    }
                },
                SyntaxKind.CastExpression);
        }