The rule to check do if and when statements in the code don't contain some
expressions. Checked things:
- Empty statements.
Ifandwhenstatements, which contains onlydiscardstatement. A branch
elseafter a finishing statement likereturn,continue,breakorraise. Example:if a == 1: return else: doSomething()A negative condition in
ifandwhenstatements with a branchelse. Example:if a != 1: doSomething() else: doSomething2()The maximum and minimum amount of
ifandwhenstatements' branches. The check must be set explicitly, it isn't performed when option all is set.
The syntax in a configuration file is:
[ruleType] ?not? ifStatements [checkType] [amount]
- ruleType is the type of rule which will be executed. Proper values are:
check, search, count and fix. For more information about the types of
rules, please refer to the program's documentation. Check type will raise
an error if there is a
iforwhenstatement which violates any of the checks. Search type will list all statements which violates any of checks or raise an error if nothing found. Count type will simply list the amount of the statements which violates the checks. Fix type will try to fix the code which violates checks: will remove empty statements, move outside theiforwhenblock code after finishing statement or replace negative condition in the statement with positive and move the code blocks. Fix type not works with negation. - optional word not means negation for the rule. Adding word not will
change to inform only about the
ifandwhenstatements which not violate the checks. Probably useable only with search and count type of rule. - ifStatements is the name of the rule. It is case-insensitive, thus it can be set as ifstatements, ifstatements or iFsTaTeMeNts.
- checkType is the type of checks to perform on the
ifandwhenstatements. Proper values are: all, negative, moveable, empty, min and max. Setting it to all will perform all rule's checks on statements except for the check for maximum and minimum amount of branches. Negative value will check only if theifandwhenstatements don't have a negative condition with branchelse. Moveable value will check only if the content ofelsebranch can be moved outside the statement. Empty value will check if theifandwhenstatements doesn't contain only adiscardstatement. Min value will check if allifandwhenstatements have at least the selected amount of branches. Max value will check if theifandwhenstatements have maximum the selected amount of branches. - amount parameter is required only for min and max types of checks and
it is ignored for another. It is desired amount of branches for the
ifandwhenstatements, minimal or maximum, depends on check's type.
Disabling the rule
It is possible to disable the rule for a selected part of the checked code by using pragma ruleOff: "ifStatements" in the code before it. For example, if the rule should be disabled for procedure the statement, the full declaration of it should be:
{.ruleOff: "ifStatements".}
if a == 1:
echo a
To enable the rule again, the pragma ruleOn: "ifStatements" should be added in the code before it. For example, if the rule should be re-enabled for the statement, the full declaration should be:
{.ruleOn: "ifStatements".}
if a == 1:
echo a
Examples
Check if all
ifandwhenstatements are correct:check ifStatements allRemove all empty
ifandwhenstatements:fix ifStatements emptyCheck if all
ifandwhenstatements have at least 3 branches:check ifStatements min 3