Beispiel #1
0
        public static void AnalyzeEqualsExpression(SyntaxNodeAnalysisContext context)
        {
            var equalsExpression = (BinaryExpressionSyntax)context.Node;

            ExpressionSyntax left = equalsExpression.Left;

            if (left?.IsMissing == false)
            {
                ExpressionSyntax right = equalsExpression.Right;

                if (right?.IsMissing == false)
                {
                    SemanticModel     semanticModel     = context.SemanticModel;
                    CancellationToken cancellationToken = context.CancellationToken;

                    if (CSharpAnalysis.IsEmptyString(left, semanticModel, cancellationToken))
                    {
                        if (IsString(right, semanticModel, cancellationToken))
                        {
                            ReportDiagnostic(context, equalsExpression);
                        }
                    }
                    else if (CSharpAnalysis.IsEmptyString(right, semanticModel, cancellationToken))
                    {
                        if (IsString(left, semanticModel, cancellationToken))
                        {
                            ReportDiagnostic(context, equalsExpression);
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public static async Task <Document> RefactorAsync(
            Document document,
            BinaryExpressionSyntax binaryExpression,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            ExpressionSyntax left = binaryExpression.Left;

            ExpressionSyntax right = binaryExpression.Right;

            BinaryExpressionSyntax newNode = binaryExpression;

            if (CSharpAnalysis.IsEmptyString(left, semanticModel, cancellationToken))
            {
                newNode = binaryExpression
                          .WithLeft(ZeroLiteralExpression())
                          .WithRight(CreateConditionalAccess(right));
            }
            else if (CSharpAnalysis.IsEmptyString(right, semanticModel, cancellationToken))
            {
                newNode = binaryExpression
                          .WithLeft(CreateConditionalAccess(left))
                          .WithRight(ZeroLiteralExpression());
            }
            else
            {
                Debug.Assert(false, binaryExpression.ToString());
                return(document);
            }

            newNode = newNode.WithTriviaFrom(binaryExpression).WithFormatterAnnotation();

            return(await document.ReplaceNodeAsync(binaryExpression, newNode, cancellationToken).ConfigureAwait(false));
        }
Beispiel #3
0
        public static void Analyze(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocation)
        {
            ExpressionSyntax expression = invocation.Expression;

            if (expression?.IsKind(SyntaxKind.SimpleMemberAccessExpression) == true)
            {
                var memberAccess = (MemberAccessExpressionSyntax)expression;

                ArgumentListSyntax argumentList = invocation.ArgumentList;

                if (argumentList != null)
                {
                    SeparatedSyntaxList <ArgumentSyntax> arguments = argumentList.Arguments;

                    if (arguments.Any())
                    {
                        SimpleNameSyntax name = memberAccess.Name;

                        if (name?.Identifier.ValueText == "Join")
                        {
                            SemanticModel     semanticModel     = context.SemanticModel;
                            CancellationToken cancellationToken = context.CancellationToken;

                            MethodInfo info = semanticModel.GetMethodInfo(invocation, cancellationToken);

                            if (info.IsValid &&
                                info.HasName("Join") &&
                                info.IsContainingType(SpecialType.System_String) &&
                                info.IsPublic &&
                                info.IsStatic &&
                                info.IsReturnType(SpecialType.System_String) &&
                                !info.IsGenericMethod &&
                                !info.IsExtensionMethod)
                            {
                                ImmutableArray <IParameterSymbol> parameters = info.Parameters;

                                if (parameters.Length == 2 &&
                                    parameters[0].Type.IsString())
                                {
                                    IParameterSymbol parameter = parameters[1];

                                    if (parameter.IsParamsOf(SpecialType.System_String) ||
                                        parameter.IsParamsOf(SpecialType.System_Object) ||
                                        parameter.Type.IsConstructedFromIEnumerableOfT())
                                    {
                                        ArgumentSyntax   firstArgument      = arguments.First();
                                        ExpressionSyntax argumentExpression = firstArgument.Expression;

                                        if (argumentExpression != null &&
                                            CSharpAnalysis.IsEmptyString(argumentExpression, semanticModel, cancellationToken) &&
                                            !invocation.ContainsDirectives(TextSpan.FromBounds(invocation.SpanStart, firstArgument.Span.End)))
                                        {
                                            context.ReportDiagnostic(
                                                DiagnosticDescriptors.CallStringConcatInsteadOfStringJoin,
                                                name);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }