cmisra

How to fix MISRA issue 14.3


I have this part of code, in the codebase, and while running coverity I have come up with an MISRA issue 14.3.

typedef uint8_t INSTANCE;

#define INSTANCE_1  (0U)
#define INSTANCE_2  (1U)
...
#define INSTANCE_11 (11U)
int fun(INSTANCE x)
{
  // I am getting the MISRA issue Controlling expression "0U <= x" is invariant.
  if (INSTANCE_1 <= x && x <= INSTANCE_11) {
          do_something()
  }
}

Not sure how to fix this MISRA issue 14.3.


Solution

  • As your INSTANCE variable is of type uint8_t and INSTANCE_1 is defined as 0U, there is no way that INSTANCE is less than 0.

    int fun(INSTANCE x)
    {
      if (INSTANCE_1 <= x && x <= INSTANCE_11) {
         do_something();
      }
    }
    

    To fix the MISRA violation in this specific example you could simply remove the condition INSTANCE_1 <= x from the if statement.

    int fun(INSTANCE x)
    {
      if (x <= INSTANCE_11) {
         do_something();
      }
    }