Exemplo n.º 1
0
 /// <summary>
 /// Updates the given SimpleNameSyntax node with the given identifier token.
 /// This function is a wrapper that calls WithIdentifier on derived syntax nodes.
 /// </summary>
 /// <param name="simpleName"></param>
 /// <param name="identifier"></param>
 /// <returns>The given simple name updated with the given identifier.</returns>
 public static SimpleNameSyntax WithIdentifier(
     this SimpleNameSyntax simpleName,
     SyntaxToken identifier
     )
 {
     return(simpleName.Kind() == SyntaxKind.IdentifierName
       ? (SimpleNameSyntax)((IdentifierNameSyntax)simpleName).WithIdentifier(identifier)
       : (SimpleNameSyntax)((GenericNameSyntax)simpleName).WithIdentifier(identifier));
 }
        private static bool IsPropertyOfNullableOfT(ExpressionSyntax expression, string name, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            if (expression?.Kind() == SyntaxKind.SimpleMemberAccessExpression)
            {
                var memberAccessExpression = (MemberAccessExpressionSyntax)expression;

                SimpleNameSyntax simpleName = memberAccessExpression.Name;

                if (simpleName?.Kind() == SyntaxKind.IdentifierName)
                {
                    var identifierName = (IdentifierNameSyntax)simpleName;

                    return(string.Equals(identifierName.Identifier.ValueText, name, StringComparison.Ordinal) &&
                           SyntaxUtility.IsPropertyOfNullableOfT(expression, name, semanticModel, cancellationToken));
                }
            }

            return(false);
        }
Exemplo n.º 3
0
        private static void AnalyzeBinaryExpression(SyntaxNodeAnalysisContext context)
        {
            var binaryExpression = (BinaryExpressionSyntax)context.Node;

            BinaryExpressionInfo binaryExpressionInfo = SyntaxInfo.BinaryExpressionInfo(binaryExpression);

            if (!binaryExpressionInfo.Success)
            {
                return;
            }

            if (binaryExpressionInfo.Right.Kind() != SyntaxKind.TrueLiteralExpression)
            {
                return;
            }

            ExpressionSyntax left = binaryExpressionInfo.Left;

            if (left.Kind() != SyntaxKind.ConditionalAccessExpression)
            {
                return;
            }

            var conditionalAccess = (ConditionalAccessExpressionSyntax)left;

            ExpressionSyntax whenNotNull = conditionalAccess.WhenNotNull;

            if (whenNotNull.Kind() != SyntaxKind.InvocationExpression)
            {
                return;
            }

            var invocationExpression = (InvocationExpressionSyntax)whenNotNull;

            if (invocationExpression.ArgumentList.Arguments.Count != 1)
            {
                return;
            }

            ExpressionSyntax expression = invocationExpression.Expression;

            if (expression.Kind() != SyntaxKind.MemberBindingExpression)
            {
                return;
            }

            var memberBindingExpression = (MemberBindingExpressionSyntax)expression;

            SimpleNameSyntax name = memberBindingExpression.Name;

            if (name.Kind() != SyntaxKind.IdentifierName)
            {
                return;
            }

            var identifierName = (IdentifierNameSyntax)name;

            if (!string.Equals(identifierName.Identifier.ValueText, "IsKind", StringComparison.Ordinal))
            {
                return;
            }

            ISymbol symbol = context.SemanticModel.GetSymbol(invocationExpression, context.CancellationToken);

            if (symbol?.Kind != SymbolKind.Method)
            {
                return;
            }

            var methodSymbol = (IMethodSymbol)symbol;

            if (methodSymbol.MethodKind != MethodKind.ReducedExtension)
            {
                return;
            }

            if (methodSymbol.ReturnType.SpecialType != SpecialType.System_Boolean)
            {
                return;
            }

            if (methodSymbol.ContainingType?.HasMetadataName(RoslynMetadataNames.Microsoft_CodeAnalysis_CSharpExtensions) != true)
            {
                return;
            }

            ImmutableArray <IParameterSymbol> parameters = methodSymbol
                                                           .ReducedFrom
                                                           .Parameters;

            if (parameters.Length != 2)
            {
                return;
            }

            if (!parameters[0].Type.HasMetadataName(RoslynMetadataNames.Microsoft_CodeAnalysis_SyntaxNode))
            {
                return;
            }

            if (!parameters[1].Type.HasMetadataName(CSharpMetadataNames.Microsoft_CodeAnalysis_CSharp_SyntaxKind))
            {
                return;
            }

            DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.UnnecessaryConditionalAccess, conditionalAccess.OperatorToken);
            DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.UnnecessaryConditionalAccessFadeOut, binaryExpression.Right);
        }
Exemplo n.º 4
0
        public static string InvocationExpressionRootName(this ExpressionSyntax invocation, string[] classNames)
        {
            if (invocation == null)
            {
                return("");
            }
            if (invocation is IdentifierNameSyntax)
            {
                if ((invocation as IdentifierNameSyntax).Identifier == null)
                {
                    return("");
                }
                return((invocation as IdentifierNameSyntax).Identifier.ValueText);
            }
            foreach (SyntaxNode invchnd in invocation.ChildNodes())
            {
                if (invchnd.Kind() == SyntaxKind.SimpleMemberAccessExpression)
                {
                    ExpressionSyntax exst = (invchnd as MemberAccessExpressionSyntax).Expression;
                    if (exst != null)
                    {
                        if (exst is IdentifierNameSyntax)
                        {
                            if ((exst as IdentifierNameSyntax).Identifier == null)
                            {
                                return("");
                            }
                            if (classNames != null)
                            {
                                if (classNames.Count() > 0)
                                {
                                    SimpleNameSyntax aname = (invchnd as MemberAccessExpressionSyntax).Name;
                                    if (aname.Kind() == SyntaxKind.GenericName)
                                    {
                                        if (!"Entity".Equals(aname.Identifier.ValueText))
                                        {
                                            return("");
                                        }

                                        TypeArgumentListSyntax typeArgumentList = (aname as GenericNameSyntax).TypeArgumentList;
                                        if (typeArgumentList != null)
                                        {
                                            if (typeArgumentList.Arguments != null)
                                            {
                                                string locClassName = "";
                                                foreach (TypeSyntax ts in typeArgumentList.Arguments)
                                                {
                                                    locClassName = ts.ToString();
                                                }
                                                if (classNames.Any(i => string.Equals(i, locClassName)))
                                                {
                                                    return((exst as IdentifierNameSyntax).Identifier.ValueText);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            return("");
                        }
                        else
                        {
                            return(exst.InvocationExpressionRootName(classNames));
                        }
                    }
                }
            }
            return("");
        }