public static void Analyze(SyntaxNodeAnalysisContext context, ArgumentSyntax argument)
        {
            if (argument.Expression?.IsKind(SyntaxKind.StringLiteralExpression) != true)
            {
                return;
            }

            using (IEnumerator <ParameterSyntax> en = GetParametersInScope(argument).GetEnumerator())
            {
                if (en.MoveNext())
                {
                    var literalExpression = (LiteralExpressionSyntax)argument.Expression;

                    string text = (literalExpression).Token.ValueText;

                    if (ExistsParameterWithName(en, text))
                    {
                        IParameterSymbol parameterSymbol = GetParameterSymbol(argument, context.SemanticModel);

                        if (parameterSymbol != null &&
                            (IdentifierEquals(parameterSymbol.Name, "paramName") ||
                             IdentifierEquals(parameterSymbol.Name, "parameterName")))
                        {
                            ReportDiagnostic(context, literalExpression, text);
                        }
                    }
                }
                else
                {
                    AccessorDeclarationSyntax setter = argument
                                                       .FirstAncestor <AccessorDeclarationSyntax>();

                    if (setter?.IsKind(SyntaxKind.SetAccessorDeclaration) == true)
                    {
                        PropertyDeclarationSyntax property = setter
                                                             .FirstAncestor <PropertyDeclarationSyntax>();

                        if (property != null)
                        {
                            var literalExpression = (LiteralExpressionSyntax)argument.Expression;

                            string text = (literalExpression).Token.ValueText;

                            if (IdentifierEquals(property.Identifier.ValueText, text))
                            {
                                IParameterSymbol parameterSymbol = GetParameterSymbol(argument, context.SemanticModel);

                                if (parameterSymbol != null &&
                                    IdentifierEquals(parameterSymbol.Name, "propertyName"))
                                {
                                    ReportDiagnostic(context, literalExpression, text);
                                }
                            }
                        }
                    }
                }
            }
        }
        public static void Analyze(SyntaxNodeAnalysisContext context, ArgumentSyntax argument)
        {
            ExpressionSyntax expression = argument.Expression;

            if (expression?.IsKind(SyntaxKind.StringLiteralExpression) == true)
            {
                using (IEnumerator <ParameterSyntax> en = GetParametersInScope(argument).GetEnumerator())
                {
                    if (en.MoveNext())
                    {
                        var literalExpression = (LiteralExpressionSyntax)expression;

                        string text = literalExpression.Token.ValueText;

                        if (ExistsParameterWithName(en, text))
                        {
                            IParameterSymbol parameterSymbol = context.SemanticModel.DetermineParameter(argument, allowParams: true, allowCandidate: false, cancellationToken: context.CancellationToken);

                            if (parameterSymbol != null &&
                                (NameEquals(parameterSymbol.Name, "paramName") ||
                                 NameEquals(parameterSymbol.Name, "parameterName")))
                            {
                                ReportDiagnostic(context, literalExpression, text);
                            }
                        }
                    }
                    else
                    {
                        AccessorDeclarationSyntax accessor = argument.FirstAncestor <AccessorDeclarationSyntax>();

                        if (accessor?.IsKind(SyntaxKind.SetAccessorDeclaration) == true)
                        {
                            PropertyDeclarationSyntax property = accessor.FirstAncestor <PropertyDeclarationSyntax>();

                            if (property != null)
                            {
                                var literalExpression = (LiteralExpressionSyntax)expression;

                                string text = literalExpression.Token.ValueText;

                                if (NameEquals(property.Identifier.ValueText, text))
                                {
                                    IParameterSymbol parameterSymbol = context.SemanticModel.DetermineParameter(argument, allowParams: true, allowCandidate: false, cancellationToken: context.CancellationToken);

                                    if (parameterSymbol != null &&
                                        NameEquals(parameterSymbol.Name, "propertyName"))
                                    {
                                        ReportDiagnostic(context, literalExpression, text);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        internal static bool TryGetParameter(this ArgumentSyntax argument, SemanticModel semanticModel, CancellationToken cancellationToken, out IParameterSymbol result)
        {
            var invocation = (SyntaxNode)argument.FirstAncestor <InvocationExpressionSyntax>() ??
                             argument.FirstAncestor <ConstructorInitializerSyntax>();
            var method = (IMethodSymbol)semanticModel.GetSymbolSafe(invocation, cancellationToken);

            result = null;
            if (argument == null ||
                method?.Parameters == null)
            {
                return(false);
            }

            if (argument.NameColon == null)
            {
                var index = argument.FirstAncestorOrSelf <ArgumentListSyntax>()
                            .Arguments.IndexOf(argument);
                if (method.Parameters.TryElementAt(index, out result))
                {
                    return(true);
                }

                var temp = method.Parameters[method.Parameters.Length - 1];
                if (temp.IsParams)
                {
                    result = temp;
                    return(true);
                }

                return(false);
            }

            foreach (var candidate in method.Parameters)
            {
                if (candidate.Name == argument.NameColon.Name.Identifier.ValueText)
                {
                    result = candidate;
                    return(true);
                }
            }

            return(false);
        }
Beispiel #4
0
        private static bool TryGetConstructor(ArgumentSyntax argument, SemanticModel semanticModel, CancellationToken cancellationToken, out IMethodSymbol ctor)
        {
            var objectCreation = argument.FirstAncestor <ObjectCreationExpressionSyntax>();

            if (objectCreation != null)
            {
                ctor = semanticModel.GetSymbolSafe(objectCreation, cancellationToken) as IMethodSymbol;
                return(ctor != null);
            }

            var initializer = argument.FirstAncestor <ConstructorInitializerSyntax>();

            if (initializer != null)
            {
                ctor = semanticModel.GetSymbolSafe(initializer, cancellationToken);
                return(ctor != null);
            }

            ctor = null;
            return(false);
        }
        /// <summary>
        /// Check if any path returns a created IDisposable
        /// </summary>
        internal static Result IsCreation(ArgumentSyntax candidate, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            if (candidate == null)
            {
                return(Result.No);
            }

            Debug.Assert(!candidate.RefOrOutKeyword.IsKind(SyntaxKind.None), "Only valid for ref or out parameter.");
            var invocation = candidate.FirstAncestor <InvocationExpressionSyntax>();

            if (invocation.TryGetMatchingParameter(candidate, semanticModel, cancellationToken, out IParameterSymbol parameter))
            {
                return(IsAssignedWithCreated(parameter, null, semanticModel, cancellationToken));
            }

            return(Result.Unknown);
        }
        private static void Analyze(SyntaxNodeAnalysisContext context, ArgumentSyntax argument, ExpressionSyntax expression)
        {
            AccessorDeclarationSyntax accessor = argument.FirstAncestor <AccessorDeclarationSyntax>();

            if (accessor?.IsKind(SyntaxKind.SetAccessorDeclaration) != true)
            {
                return;
            }

            PropertyDeclarationSyntax property = accessor.FirstAncestor <PropertyDeclarationSyntax>();

            if (property == null)
            {
                return;
            }

            var literalExpression = (LiteralExpressionSyntax)expression;

            string text = literalExpression.Token.ValueText;

            if (!string.Equals(property.Identifier.ValueText, text, StringComparison.Ordinal))
            {
                return;
            }

            IParameterSymbol parameterSymbol = context.SemanticModel.DetermineParameter(argument, allowParams: true, allowCandidate: false, cancellationToken: context.CancellationToken);

            if (parameterSymbol == null)
            {
                return;
            }

            if (!string.Equals(parameterSymbol.Name, "propertyName", StringComparison.Ordinal))
            {
                return;
            }

            ReportDiagnostic(context, literalExpression, property.Identifier.Text);
        }