Main Content

MISRA C:2012 Rule 14.3

Controlling expressions shall not be invariant

Description

Rule Definition

Controlling expressions shall not be invariant.

Rationale

If the controlling expression, for example an if condition, has a constant value, the non-changing value can point to a programming error.

Polyspace Implementation

The checker reports a violation if the controlling expression of a control statement is always true or false. The control statements that Polyspace® checks include:

  • if, switch

  • for, while, do-while

  • Ternary (?:) operator

Polyspace Bug Finder™ flags some violations of MISRA C™ 14.3 through the Dead code and Useless if checkers.

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

In this example, a 16-bit unsigned integer u16var is compared to 0xffffu in an if statement. because u16var is a 16-bit unsigned integer, it cannot have a value greater than 0xffffu. The if statement is always true. Polyspace reports a violation of this rule.

#include<stdint.h>

uint16_t u16var;
void foo(){
	if(u16var<=0xffffu){  //Noncompliant
		//...
	}
}

Check Information

Group: Control Statement Expressions
Category: Required
AGC Category: Required

Version History

expand all