Why did my tool throw a MISRA error here?

前端 未结 4 1722
死守一世寂寞
死守一世寂寞 2021-01-18 16:18

What can I do to avoid MISRA giving this error for the code below? I tried casting with (unit16_t). But then it didn\'t allow an explicit conversion.

Illegal implici

4条回答
  •  甜味超标
    2021-01-18 16:26

    The implicit conversion is performed before the multiplication, for the multiplication. Maybe an explicit conversion right before the multiplication shuts up your tool

    uint16_t basic_units = (unsigned)rate * (unsigned)percentage;
    

    The resulting unsigned value, should be implicitly converted to uint16_t with no warnings. If your tool chooses to be a PITA about this too, try another explicit conversion:

    uint16_t basic_units = (uint16_t)((unsigned)rate * (unsigned)percentage);
    

提交回复
热议问题