public override void Initialize(AnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var lessThan = (BinaryExpressionSyntax)c.Node;
                int constValue;
                if (SillyBitwiseOperation.TryGetConstantIntValue(lessThan.Left, out constValue) &&
                    constValue == 0 &&
                    IsIndexOfCall(lessThan.Right, c.SemanticModel))
                {
                    c.ReportDiagnostic(Diagnostic.Create(Rule, Location.Create(lessThan.SyntaxTree,
                                                                               TextSpan.FromBounds(lessThan.Left.SpanStart, lessThan.OperatorToken.Span.End))));
                }
            },
                SyntaxKind.LessThanExpression);

            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var greaterThan = (BinaryExpressionSyntax)c.Node;
                int constValue;
                if (SillyBitwiseOperation.TryGetConstantIntValue(greaterThan.Right, out constValue) &&
                    constValue == 0 &&
                    IsIndexOfCall(greaterThan.Left, c.SemanticModel))
                {
                    c.ReportDiagnostic(Diagnostic.Create(Rule, Location.Create(greaterThan.SyntaxTree,
                                                                               TextSpan.FromBounds(greaterThan.OperatorToken.SpanStart, greaterThan.Right.Span.End))));
                }
            },
                SyntaxKind.GreaterThanExpression);
        }
예제 #2
0
        private static bool CheckValueTypeDefaultValueInitializer(VariableDeclaratorSyntax variable, IFieldSymbol variableSymbol)
        {
            if (!variableSymbol.Type.IsValueType)
            {
                return(false);
            }

            switch (variableSymbol.Type.SpecialType)
            {
            case SpecialType.System_Boolean:
                return(EquivalenceChecker.AreEquivalent(variable.Initializer.Value, FalseExpression));

            case SpecialType.System_Char:
            case SpecialType.System_Byte:
            case SpecialType.System_Decimal:
            case SpecialType.System_Double:
            case SpecialType.System_Int16:
            case SpecialType.System_Int32:
            case SpecialType.System_Int64:
            case SpecialType.System_SByte:
            case SpecialType.System_Single:
            case SpecialType.System_UInt16:
            case SpecialType.System_UInt32:
            case SpecialType.System_UInt64:
                int constantValue;
                return(SillyBitwiseOperation.TryGetConstantIntValue(variable.Initializer.Value, out constantValue) &&
                       constantValue == 0);

            default:
                return(false);
            }
        }
예제 #3
0
 private static bool CheckExpression(ExpressionSyntax constant, ExpressionSyntax modulus, SemanticModel semanticModel,
                                     out int constantValue)
 {
     return(SillyBitwiseOperation.TryGetConstantIntValue(constant, out constantValue) &&
            constantValue != 0 &&
            ExpressionIsModulus(modulus) &&
            !ExpressionIsNonNegative(modulus, semanticModel));
 }
        private static void CheckCountOne(ExpressionSyntax one, ExpressionSyntax count, SyntaxNodeAnalysisContext c)
        {
            Location reportLocation;
            string   typeArgument;
            int      value;

            if (SillyBitwiseOperation.TryGetConstantIntValue(one, out value) &&
                value == 1 &&
                TryGetCountCall(count, c.SemanticModel, out reportLocation, out typeArgument))
            {
                c.ReportDiagnostic(Diagnostic.Create(Rule, reportLocation, typeArgument));
            }
        }
예제 #5
0
        public override void Initialize(AnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var expression = (BinaryExpressionSyntax)c.Node;
                int value;
                if (expression.IsKind(SyntaxKind.LeftShiftExpression) &&
                    SillyBitwiseOperation.TryGetConstantIntValue(expression.Right, out value) &&
                    value == 1)
                {
                    return;
                }

                if (EquivalenceChecker.AreEquivalent(expression.Left, expression.Right))
                {
                    c.ReportDiagnostic(Diagnostic.Create(Rule, c.Node.GetLocation(), expression.OperatorToken));
                }
            },
                SyntaxElementsToCheck);
        }