主要内容

MISRA C:2025 Rule 14.3

R2026b

Controlling expressions shall not be invariant

Since R2026b

Description

Controlling expressions shall not be invariant 1 .

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

The checker also flags expressions that evaluate to a constant through arithmetic or logical operations on literals, such as !0, 1 + 1, or 1 == 1.

Polyspace Bug Finder™ flags some violations of MISRA C:2025 Rule 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
		//...
	}
       if(0u ==1u){  //Noncompliant 
		//...
	} 
}

Similarly, the unsigned integer 0u cannot be equal to 1u. Polyspace reports a violation on the use of this comparison as the condition of the if statement.

Check Information

Group: Control Statement Expressions
Category: Required
AGC Category: Required
PQL Name: std.misra_c_2025.R14_3

Version History

Introduced in R2026b

expand all


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C:2025

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.