I have below function to check some count value and update the final count.
uint16 final_count = 0U;
uint8 count1 = 0U;
uint8 count2 = 0U;
uint8 count3 = 0U;
uint8 count4 = 0U;
void test(void)
{
uint8 input = 0U;
input = get_input(); //Input
count1 = get_count1(); //count1
count2 = get_count2(); //count2
count3 = get_count3(); //count3
count4 = get_count4(); //count4
if(input == 1U)
{
final_count= count1 + count2; // Both warnings Here
}
else if(input == 2U)
{
final_count= count2 + count3; // Both warnings Here
}
else
{
final_count= count1 + count2 + count3 + count4; // Both warnings Here
}
}
During the MISRA check, I am getting below errors in the if else
A composite expression of 'essentially unsigned' type (unsigned char) is being converted to wider unsigned type, 'unsigned short' on assignment.
Integral promotion : unsigned char promoted to signed int.
I have tried to solve by below method
final_count = (uint16)(count1 + count2);
final_count = (uint16)(count3 + count4);
final_count = (uint16)(count1 + count2 + count3 + count4);
But this couldn't able to solve above MISRA warnings. What is the exact problem happening here? Any suggestion to solve these warnings with out temporary variables?