/// <summary>
        /// Checks the statement, which is a parent of a block statement, to make sure that it is not empty.
        /// </summary>
        /// <param name="statement">The statement to check.</param>
        /// <returns>Returns true if the statement was empty.</returns>
        private static bool IsEmptyParentOfBlockStatement(Statement statement)
        {
            Param.AssertNotNull(statement, "statement");

            // Find the block statement under this statement.
            for (Statement childStatement = statement.FindFirstChildStatement(); childStatement != null; childStatement = childStatement.FindNextSiblingStatement())
            {
                if (childStatement.StatementType == StatementType.Block)
                {
                    if (childStatement.Children.StatementCount == 0)
                    {
                        return true;
                    }

                    break;
                }
            }

            return false;
        }
Exemple #2
0
        /// <summary>
        /// Processes the given statement.
        /// </summary>
        /// <param name="statement">The statement to process.</param>
        /// <param name="element">The parent element.</param>
        /// <param name="validPrefixes">The list of acceptable Hungarian-type prefixes.</param>
        private void ProcessStatement(Statement statement, Element element, Dictionary<string, string> validPrefixes)
        {
            Param.AssertNotNull(statement, "statement");
            Param.AssertNotNull(element, "element");
            Param.AssertNotNull(validPrefixes, "validPrefixes");

            // Check the statement's variables.
            VariableCollection variables = statement.Variables;

            if (variables != null)
            {
                foreach (IVariable variable in variables)
                {
                    this.CheckMethodVariablePrefix(variable, element, validPrefixes);
                    this.CheckUnderscores(element, variables);
                }
            }

            // Check the expressions under this statement.
            if (statement.Children.ExpressionCount > 0)
            {
                for (Expression expression = statement.FindFirstChildExpression(); expression != null; expression = expression.FindNextSiblingExpression())
                {
                    this.ProcessExpression(expression, element, validPrefixes);
                }
            }

            // Check each of the statements under this statement.
            if (statement.Children.StatementCount > 0)
            {
                for (Statement childStatement = statement.FindFirstChildStatement(); childStatement != null; childStatement = childStatement.FindNextSiblingStatement())
                {
                    this.ProcessStatement(childStatement, element, validPrefixes);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Checks to make sure that if, while, for, and foreach statements are followed by a curly bracket block.
        /// </summary>
        /// <param name="parentElement">The element containing the statement.</param>
        /// <param name="statement">The statement which may or may not be missing the child block</param>
        /// <param name="embeddedStatement">The statement embedded within the if, while, for, or foreach statement.</param>
        /// <param name="statementType">The user-friendly type of the statement.</param>
        /// <param name="allowStacks">True to allow statements of the same type to be stacked together where only the last statement in the stack has curly brackets.</param>
        private void CheckMissingBlock(Element parentElement, Statement statement, Statement embeddedStatement, string statementType, bool allowStacks)
        {
            Param.AssertNotNull(parentElement, "parentElement");
            Param.AssertNotNull(statement, "statement");
            Param.Ignore(embeddedStatement);
            Param.AssertValidString(statementType, "statementType");
            Param.Ignore(allowStacks);

            if (embeddedStatement != null && embeddedStatement.StatementType != StatementType.Block)
            {
                Statement firstChildStatement = statement.FindFirstChildStatement();

                if (!allowStacks ||
                    firstChildStatement == null ||
                    firstChildStatement.StatementType != statement.StatementType)
                {
                    this.AddViolation(parentElement, embeddedStatement.LineNumber, Rules.CurlyBracketsMustNotBeOmitted, statementType);
                }
            }
        }