Skip to main content

IAR Embedded Workbench for RX 5.20

RED-cond-const-expr

In this section:
Synopsis

A conditional expression with a constant value

Enabled by default

No

Severity/Certainty

Low/Medium

lowmedium.png
Full description

A non-trivial expression composed only of constants is used as the truth value in a conditional expression. The condition will either always or never be true, and thus program flow is deterministic, making the test redundant. This check assumes that trivial conditions, such as using a const variable or literal directly, are intentional. It is easy to see if they are indeed unintentional.

Coding standards
CWE 570

Expression is Always False

CWE 571

Expression is Always True

Code examples

The following code example fails the check and will give a warning:

int foo(int x){
  while (1+1){
  };
}

int foo2(int x){
  for(x = 0; 0 < 10; x++){
  };
}

The following code example passes the check and will not give a warning about this issue:

int foo(int x){

  while (foo(foo(3))){
    x++;
  }
  return x;
}


int foo2(int x){
  while (0){ // valid usage
    
  }
  return x;
}