Ejemplo n.º 1
0
        public static IEnumerable <ValidationRule> GetValidationRules()
        {
            yield return(ValidationRule.CheckContractStatement(
                             s =>
            {
                if (s.IsPrecondition && IsContainingMethodAsync(s.Statement))
                {
                    return ValidationResult.CreateCustomWarning(s.Statement,
                                                                MalformedContractCustomWarning.PreconditionInAsyncMethod);
                }
                return ValidationResult.CreateNoError(s.Statement);
            }));

            yield return(ValidationRule.CheckContractStatement(
                             s =>
            {
                if (s.IsPrecondition && IsContainingMethodContainsIteratorBlock(s.Statement))
                {
                    return ValidationResult.CreateCustomWarning(s.Statement,
                                                                MalformedContractCustomWarning.PreconditionInMethodWithIteratorBlock);
                }
                return ValidationResult.CreateNoError(s.Statement);
            }));
        }
Ejemplo n.º 2
0
        private static IEnumerable <ValidationRule> GetValidationRules()
        {
            yield return(ValidationRule.CheckStatement(
                             s =>
            {
                // void-return methods are prohibited by CC compiler
                if (IsVoidReturnMethod(s))
                {
                    return ValidationResult.CreateError(s, MalformedContractError.VoidReturnMethodCall);
                }
                return ValidationResult.CreateNoError(s);
            }));

            yield return(ValidationRule.CheckStatement(
                             s =>
            {
                // Non-void return methods lead to warning from the compiler
                if (IsNonVoidReturnMethod(s))
                {
                    return ValidationResult.CreateWarning(s, MalformedContractWarning.NonVoidReturnMethodCall);
                }
                return ValidationResult.CreateNoError(s);
            }));

            yield return(ValidationRule.CheckStatement(
                             s =>
            {
                // Assignments are forbidden in contract block
                if (IsAssignmentStatement(s))
                {
                    return ValidationResult.CreateError(s, MalformedContractError.AssignmentInContractBlock);
                }
                return ValidationResult.CreateNoError(s);
            }));

            yield return(ValidationRule.CheckCodeContractStatement(
                             s =>
            {
                // Assert/Assume are forbidden in contract block
                if ((s.StatementType == CodeContractStatementType.Assert ||
                     s.StatementType == CodeContractStatementType.Assume))
                {
                    return ValidationResult.CreateError(s.Statement, MalformedContractError.AssertOrAssumeInContractBlock);
                }
                return ValidationResult.CreateNoError(s.Statement);
            }));

            yield return(ValidationRule.CheckContractBlock(
                             (currentStatement, contractBlock) =>
            {
                // Ensures/Ensures on throw should not be before requires
                if (currentStatement.IsPostcondition && HasPreconditionAfterCurrentStatement(contractBlock, currentStatement))
                {
                    return ValidationResult.CreateError(currentStatement.Statement,
                                                        MalformedContractError.RequiresAfterEnsures);
                }
                return ValidationResult.CreateNoError(currentStatement.Statement);
            }));

            yield return(ValidationRule.CheckContractBlock(
                             (currentStatement, contractBlock) =>
            {
                // Ensures/Ensures on throw should not be before requires
                if (currentStatement.IsPrecondition && HasPostconditionsBeforeCurentStatement(contractBlock, currentStatement))
                {
                    return ValidationResult.CreateError(currentStatement.Statement,
                                                        MalformedContractError.RequiresAfterEnsures);
                }
                return ValidationResult.CreateNoError(currentStatement.Statement);
            }));

            yield return(ValidationRule.CheckContractBlock(
                             (currentStatement, contractBlock) =>
            {
                // Ensures/Ensures on throw should not be before requires
                if ((currentStatement.IsPrecondition ||
                     currentStatement.IsPostcondition) &&
                    HasEndContractBlockBeforeCurrentStatement(contractBlock, currentStatement))
                {
                    return ValidationResult.CreateError(currentStatement.Statement,
                                                        MalformedContractError.ReqruiesOrEnsuresAfterEndContractBlock);
                }
                return ValidationResult.CreateNoError(currentStatement.Statement);
            }));

            yield return(ValidationRule.CheckContractBlock(
                             (currentStatement, contractBlock) =>
            {
                // EndContractBlock should be only one!
                if (currentStatement.IsEndContractBlock && HasEndContractBlockBeforeCurrentStatement(contractBlock, currentStatement))
                {
                    return ValidationResult.CreateError(currentStatement.Statement,
                                                        MalformedContractError.DuplicatedEndContractBlock);
                }
                return ValidationResult.CreateNoError(currentStatement.Statement);
            }));
        }
Ejemplo n.º 3
0
 public static IEnumerable <ValidationRule> GetValidationRules()
 {
     yield return(ValidationRule.CheckCodeContractStatement(
                      CheckCompatibilityOfContractResultWithMethodReturnType));
 }