Reputation: 1694
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?
Upvotes: 0
Views: 2105
Reputation: 214730
MISRA-C:2004 didn't treat boolean types as a special case, they were just another small integer type like char
etc. Notably, it did not support bool
either since it didn't support C99.
All if this was fixed in MISRA-C:2012, where your home-brewed type would be so-called essentially boolean. There's no requirement in MISRA-C:2012 that you must use bool
, although this is recommended, but the guidelines tolerate the use of "home-brewed booleans", given that you can somehow tell your static analyser which type it is.
But since you are using an older version of MISRA-C, booleanVal = ( maskedVal != 0U );
is an assignment to narrower type from int
, and also from a signed type to an unsigned type.
This is a violation of MISRA-C:2004 rule 10.1, but perfectly fine in MISRA-C:2012.
Notably, you also have an implicit promotion from uint8_t
to unsigned int
, although that shouldn't violate any MISRA rule.
Upvotes: 2
Reputation: 34585
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.
Upvotes: 5