简体   繁体   中英

Why MISRA-C disallow implicitly widening a type in some circumstances?

Implicitly widening the type of a function argument or a return expression is disallowed by MISRA-C:2004 Rule 10.1 , as illustrated in the following code snippet:

void foo1(int16_t x);

int16_t foo2(void) 
{
    int8_t s8a;
    ...
    foo1(s8a);                               /* not compliant */
    ...
    return s8a;                              /* not compliant */
}

But, in my understanding, they're no different than the assigning situation:

s16a = s8a;                                  /* compliant     */

What's the point? Thanks.

MISRA-C:2004 Rule 10.1 (the cited Guideline) states:

The value of an expression of integer type shall not be implicitly converted to a different underlying type if:

  1. it is not a conversion to a wider integer type of the same signedness, or
  2. ...

In the example cited, the conversion is to a wider integer type (int8_t to int16_t) so Rule 10.1 does not apply.

The expansion (of 10.1 and 10.2) explain that the purpose of the Rule is to prevent implicit conversions from wider to narrower types. There is no restriction the other way!

Your tool is broken.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM