I'm working on a project where we are appling MISRA 2004.
On most of the violations I got the reason, but one I don't understand:
Its in the if-statement with &&
and ||
operations.
Example:
uint8 getValue()
{
// Some algorithm, simplified with return 1
uint8 someValue = 1u;
return someValue;
}
int main(int argc, char *argv[]) {
uint8 currentState = 0u;
uint8 var_a = getValue();
uint8 var_b = getValue();
uint8 var_c = getValue();
uint8 var_d = getValue();
const uint8 const_a = 1u;
const uint8 const_b = 2u;
const uint8 const_c1 = 3u;
const uint8 const_c2 = 30u;
const uint8 const_d = 4u;
if ((var_a == const_a) && (var_b == const_b) && ((var_c == const_c1) || (var_c == const_c2)) && (var_d == const_d))
{
currentState = 1;
} else
{
currentState = 2;
}
}
This gives me twice the MISRA violation to rule 12.5 non-primary expression used with logical operator. Both are in the line with the IF-Statement
I don't see a problem with this if-statement, despite its a bit long.
Does anybody know, whats wrong here and how to solve that violation?
Edit: I adjusted the example a bit. I also noticed, that I get only one error if I simplify the if statement to:
if ((var_a == const_a) && ((var_c == const_c1) || (var_c == const_c2)) && (var_d == const_d))
And no violation if its changed to:
if ((var_a == const_a) && ((var_c == const_c1) || (var_c == const_c2)))
var_X
andconst_X
in your case? – Locomobileif (x && !y); // Noncompliant
– Titus