Why did my tool throw a MISRA error here?

前端 未结 4 1720
死守一世寂寞
死守一世寂寞 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:29

    If you try casting your operands using C-style casting, you might just give your tool something else to complain about. So, instead of doing this:

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

    You may need to do this:

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

提交回复
热议问题