Synopsis: | The condition (or guard) of a selective alternative shall be a logical operator, an equality operator or a relational expression (and not be an assignment). Exception is made for boolean type. For boolean type no relational expression is required. |
Language: | C |
Severity Level: | 6 |
Category: | Statement and Blocks |
Description: |
Justification
Example 1 if (i = a) /* WRONG: assignment as well as a test */ { /* Any statement */ } Example 2 if (i) /* WRONG */ { /* Any statement */ } Example 3 if (i == 1) /* RIGHT: equality operator */ { /* Any statement */ } Example 4 if (fp = fopen("TMP", "r")) /* WRONG */ { /* Any statement */ } Example 5 if (!(fp = fopen("TMP", "r"))) /* WRONG */ { /* Any statement */ } Example 6 fp = fopen("TMP", "r"); if (fp != NULL) /* RIGHT */ { /* Any statement */ } Example 7 bool doorclosed(void); if (doorclosed() == true) /* WRONG */ { /* Any statement */ } Example 8 bool doorclosed(void); if (doorclosed()) /* RIGHT: by exception */ { /* Any statement */ } |