13-03-2015, 08:35 AM
Your first code example will only violate rule 12.4 if CommandState and/or commData have volatile qualified types.
You would violate no rules by restructuring your code with multiple "if statements". However you should be aware that if CommandState is volatile qualified that its value could change between the tests in both versions of the code.
Two alternatives ways of writing the example would be either with a switch statement:Or by only accessing any volatiles once by use of a temporary object:
You would violate no rules by restructuring your code with multiple "if statements". However you should be aware that if CommandState is volatile qualified that its value could change between the tests in both versions of the code.
Two alternatives ways of writing the example would be either with a switch statement:
Code:
switch ( CommandState )
{
case 7U:
case 8U:
case 9U:
case 10U:
case 11U:
{
break;
}
default:
{
if ( commData == 0x24U )
{
CommandState = 0U;
}
break;
}
}
Code:
uint32_t CommandState_hold; /* or appropriate type */
uint32_t commData_hold;
CommandState_hold = CommandState;
commData_hold = commData;
if( (CommandState_hold!=7u)&&(CommandState_hold!=8u)&&(CommandState_hold!=9u)
&&(CommandState_hold!=10u)&&(CommandState_hold!=11u)&&(commData_hold==0x24u))
Posted by and on behalf of the MISRA C Working Group