Search code examples
clintmisrapc-lint

PC-Lint Misra 10.1 Error on Boolean Expression


PC-Lint version 9.00L looks at this code:

  typedef unsigned char boolean_t; //This is actually in a system header file.
  ...
  /* The rest is in the .c file I'm working on. */
  boolean_t booleanVal
  ...
  uint8_t maskedVal;
  maskedVal = 0; //real code has this assigned based on a bitwise-and
  booleanVal = ( maskedVal != 0U );

And gives this error:

booleanVal = ( maskedVal != 0U );
                                ^
"LINT: <filename> Note 960: Violates MISRA 2004 
Required Rule 10.1, Implicit conversion of integer to smaller type"

I have declared boolean_t as a strong Boolean type using -strong(B, boolean_t ) in my .lnt file.

So why is PC-Lint complaining about converting integers when I'm assigning a clearly boolean expression to a clearly boolen variable?


Solution

  • The result of ( maskedVal != 0U ) is int yet even though it is 0 or 1 MISRA complains that it is being forced into the smaller unsigned char your homebrew boolean type.

    Don't invent your own boolean type. Either use int or the formal boolean type available in modern C implementations.