﻿# V2594\. MISRA\. Controlling expressions should not be invariant\.

This diagnostic rule is based on the [MISRA](https://www.misra.org.uk/) \(Motor Industry Software Reliability Association\) software development guide\.

This rule only applies to programs written in C\. Controlling expressions in 'if', '?:', 'while', 'for', 'do', 'switch' should not be invariant, that is, controlling expressions should not always lead to executing the same code branch\. An invariant value in a controlling expression may indicate a program error\. The compiler may remove any code, unreachable due to an invariant expression\. Expressions containing 'volatile' variables are not invariant\.

Exceptions:

* 'do' loops with a controlling expression of the essential 'Boolean' type, which is evaluated as '0';
* invariants that are used to create infinite loops\.

Note\. The following invariants may be used to create infinite loops:

* literals of the essential 'Boolean' type: '1' or 'true' \(C99\);
* converting a constant literal '1' to an essential 'Boolean' type \(for example, '\(bool\) 1'\);
* 'for' loop without a controlling expression\.

Consider an example:

```cpp
void adjust(unsigned error)
{
  if (error < 0)
  {
    increase_value(-error);
  }
  else
  {
    decrease_value(error);
  }
}
```

This example illustrates the error\. The condition is always false because the function receives an unsigned integer\. As a result, the 'decrease\_value' function is always called\. The compiler may remove the code branch with the 'increase\_value' function\.