I get misra 14.4 violation saying " the condition exprecssion "0" does not have an essential boolean type"
It is a function defined as a macro
How can I fix this violation ?
#define function(a,b,c) do { \
< some -code > \
} while (0)
How can I solve this ?
I tried using while (false) and while (1 == 0) instead of while (0) But the violations was not fixed
The recommendation to use
do ... while(0)
macros was removed between MISRA C:2004 and 2012 (this might be one of the few places where I actually managed to influence the committee), because MISRA C already has a rule that only compound statements{}
are allowed. Meaning thatdo-while(0)
is a counter-productive trick that may hide MISRA violations. In a MISRA context, it is a good thing if code such asif(cond) macro();
yields a compiler error, because it isn't MISRA compliant. And the MISRA compliantif(cond) { macro(); }
will not needdo-while(0)
.Details here: What is do { } while(0) in macros and should we use it?
If you still insist to use such macros then
do { ... } while(false)
ought to silence the static analyzer.Also note that
0
in an expression which is intended to be unsigned is yet another MISRA C violation that might annoy the analyzer -0u
would be correct.