This lesson demonstrates how to create a rule that reports a violation when assignment is used in an IF statement condition. Using assignment within if statement conditions is legal (so it won’t be caught by a compiler), but using a=b
in IF statement conditions instead a==b
is a common typographical error. In these cases, using assignment would prevent the code from functioning as intended.
The rule you will create in this lesson is included with Parasoft static analysis tools as PB.TYPO.ASI. |
In this section:
Designing the rule pattern consists of creating a parent rule node and then adding qualifications to that node so that it fully expresses the code pattern that you wish to search for and remove during static analysis.
No additional qualifications are necessary for the rule to serve its purpose.
We need to specify the text printed when the rule is violated.
Avoid assignment in IF statement conditions
. Your rule now reports the specified error message when a developer uses assignment in IF statement condition. Your rule is functionally complete, but you may want to customize rule properties, such as severity, description, author, etc.. See Customizing Rule Properties for additional information.
Before you can check your rule, you need to save and enable it as described in Saving and Enabling Rules.