The rule to check do if
and when
statements in the code don't contain some
expressions. Checked things:
- Empty statements.
If
andwhen
statements, which contains onlydiscard
statement. A branch
else
after a finishing statement likereturn
,continue
,break
orraise
. Example:if a == 1: return else: doSomething()
A negative condition in
if
andwhen
statements with a branchelse
. Example:if a != 1: doSomething() else: doSomething2()
The maximum and minimum amount of
if
andwhen
statements' 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
if
orwhen
statement 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 theif
orwhen
block 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
if
andwhen
statements 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
if
andwhen
statements. 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 theif
andwhen
statements don't have a negative condition with branchelse
. Moveable value will check only if the content ofelse
branch can be moved outside the statement. Empty value will check if theif
andwhen
statements doesn't contain only adiscard
statement. Min value will check if allif
andwhen
statements have at least the selected amount of branches. Max value will check if theif
andwhen
statements 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
if
andwhen
statements, 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
if
andwhen
statements are correct:check ifStatements all
Remove all empty
if
andwhen
statements:fix ifStatements empty
Check if all
if
andwhen
statements have at least 3 branches:check ifStatements min 3