コード例 #1
0
        protected sealed override void Initialize(SonarAnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                if (c.Compilation.Options.OutputKind == OutputKind.ConsoleApplication)
                {
                    return;
                }

                var methodCall   = (InvocationExpressionSyntax)c.Node;
                var methodSymbol = c.SemanticModel.GetSymbolInfo(methodCall.Expression).Symbol;

                if (methodSymbol != null &&
                    methodSymbol.IsInType(KnownType.System_Console) &&
                    BannedConsoleMembers.Contains(methodSymbol.Name) &&
                    !CSharpDebugOnlyCodeHelper.IsInDebugBlock(c.Node) &&
                    !CSharpDebugOnlyCodeHelper.IsCallerInConditionalDebug(methodCall, c.SemanticModel))

                {
                    c.ReportDiagnosticWhenActive(Diagnostic.Create(SupportedDiagnostics[0],
                                                                   methodCall.Expression.GetLocation()));
                }
            },
                SyntaxKind.InvocationExpression);
        }
コード例 #2
0
        protected override void Initialize(SonarAnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var invocationSyntax = (InvocationExpressionSyntax)c.Node;
                if (!(c.SemanticModel.GetSymbolInfo(invocationSyntax).Symbol is IMethodSymbol methodSymbol) ||
                    invocationSyntax.ArgumentList == null)
                {
                    return;
                }

                // Calling to/from debug-only code
                if (methodSymbol.IsDiagnosticDebugMethod() ||
                    CSharpDebugOnlyCodeHelper.IsConditionalDebugMethod(methodSymbol) ||
                    CSharpDebugOnlyCodeHelper.IsCallerInConditionalDebug(invocationSyntax, c.SemanticModel))
                {
                    return;
                }

                if (methodSymbol.IsConsoleWrite() || methodSymbol.IsConsoleWriteLine())
                {
                    var firstArgument = invocationSyntax.ArgumentList.Arguments.FirstOrDefault();
                    if (IsStringLiteral(firstArgument?.Expression, c.SemanticModel))
                    {
                        c.ReportDiagnosticWhenActive(Diagnostic.Create(rule, firstArgument.GetLocation()));
                    }
                    return;
                }

                methodSymbol.Parameters
                .Merge(invocationSyntax.ArgumentList.Arguments, (parameter, syntax) => new { parameter, syntax })
                .Where(x => x.parameter != null && x.syntax != null)
                .Where(x => IsLocalizable(x.parameter))
                .Where(x => IsStringLiteral(x.syntax.Expression, c.SemanticModel))
                .ToList()
                .ForEach(x =>
                {
                    c.ReportDiagnosticWhenActive(Diagnostic.Create(rule, x.syntax.GetLocation()));
                });
            },
                SyntaxKind.InvocationExpression);

            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var assignmentSyntax = (AssignmentExpressionSyntax)c.Node;
                if (c.SemanticModel.GetSymbolInfo(assignmentSyntax.Left).Symbol is IPropertySymbol propertySymbol &&
                    IsLocalizable(propertySymbol) &&
                    IsStringLiteral(assignmentSyntax.Right, c.SemanticModel) &&
                    !CSharpDebugOnlyCodeHelper.IsCallerInConditionalDebug(assignmentSyntax, c.SemanticModel))
                {
                    c.ReportDiagnosticWhenActive(Diagnostic.Create(rule, assignmentSyntax.GetLocation()));
                }
            },

                SyntaxKind.SimpleAssignmentExpression);
        }