コード例 #1
0
        protected sealed override void Initialize(SonarAnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var invocation   = (InvocationExpressionSyntax)c.Node;
                var calledMethod = c.SemanticModel.GetSymbolInfo(invocation).Symbol as IMethodSymbol;
                if (calledMethod == null)
                {
                    return;
                }

                var memberAccess = invocation.Expression as MemberAccessExpressionSyntax;
                if (memberAccess == null)
                {
                    return;
                }

                if (calledMethod.IsInType(KnownType.System_String) &&
                    CommonCultureSpecificMethodNames.Contains(calledMethod.Name) &&
                    !calledMethod.Parameters
                    .Any(param => param.Type.IsAny(StringCultureSpecifierNames)))
                {
                    c.ReportDiagnostic(Diagnostic.Create(rule, memberAccess.Name.GetLocation(), MessageDefineLocale));
                    return;
                }

                if (calledMethod.IsInType(KnownType.System_String) &&
                    IndexLookupMethodNames.Contains(calledMethod.Name) &&
                    calledMethod.Parameters.Any(param => param.Type.SpecialType == SpecialType.System_String) &&
                    !calledMethod.Parameters.Any(param => param.Type.IsAny(StringCultureSpecifierNames)))
                {
                    c.ReportDiagnostic(Diagnostic.Create(rule, memberAccess.Name.GetLocation(), MessageDefineLocale));
                    return;
                }

                if (IsMethodOnNonIntegralOrDateTime(calledMethod) &&
                    calledMethod.Name == ToStringMethodName &&
                    !calledMethod.Parameters.Any())
                {
                    c.ReportDiagnostic(Diagnostic.Create(rule, memberAccess.Name.GetLocation(), MessageDefineLocale));
                    return;
                }

                if (calledMethod.IsInType(KnownType.System_String) &&
                    calledMethod.Name == CompareToMethodName)
                {
                    c.ReportDiagnostic(Diagnostic.Create(rule, memberAccess.Name.GetLocation(), MessageChangeCompareTo));
                    return;
                }
            },
                SyntaxKind.InvocationExpression);
        }
コード例 #2
0
        private static void ReportOnViolation(SyntaxNodeAnalysisContext context)
        {
            var invocation = (InvocationExpressionSyntax)context.Node;

            if (!(invocation.Expression is MemberAccessExpressionSyntax memberAccess))
            {
                return;
            }

            if (!(context.SemanticModel.GetSymbolInfo(invocation).Symbol is IMethodSymbol calledMethod))
            {
                return;
            }

            if (invocation.IsInExpressionTree(context.SemanticModel))
            {
                return; // We cannot specify the culture in an expression tree
            }

            if (calledMethod.IsInType(KnownType.System_String) &&
                CommonCultureSpecificMethodNames.Contains(calledMethod.Name) &&
                !calledMethod.Parameters.Any(param => param.Type.IsAny(StringCultureSpecifierNames)))
            {
                context.ReportDiagnosticWhenActive(Diagnostic.Create(rule, memberAccess.Name.GetLocation(), MessageDefineLocale));
                return;
            }

            if (calledMethod.IsInType(KnownType.System_String) &&
                IndexLookupMethodNames.Contains(calledMethod.Name) &&
                calledMethod.Parameters.Any(param => param.Type.SpecialType == SpecialType.System_String) &&
                !calledMethod.Parameters.Any(param => param.Type.IsAny(StringCultureSpecifierNames)))
            {
                context.ReportDiagnosticWhenActive(Diagnostic.Create(rule, memberAccess.Name.GetLocation(), MessageDefineLocale));
                return;
            }

            if (IsMethodOnNonIntegralOrDateTime(calledMethod) &&
                calledMethod.Name == ToStringMethodName &&
                calledMethod.Parameters.Length == 0)
            {
                context.ReportDiagnosticWhenActive(Diagnostic.Create(rule, memberAccess.Name.GetLocation(), MessageDefineLocale));
                return;
            }

            if (calledMethod.IsInType(KnownType.System_String) &&
                calledMethod.Name == CompareToMethodName)
            {
                context.ReportDiagnosticWhenActive(Diagnostic.Create(rule, memberAccess.Name.GetLocation(), MessageChangeCompareTo));
            }
        }