且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何整数类型隐式转换?

更新时间:2022-10-17 10:14:50

我会怀疑一个编译器错误。您正在使用什么编译器? 这个帖子提到编译器故障而导致米斯拉10.1采用TI公司的编译器时/ R故障。

The following code fails on a MISRA check. The concrete error message is:

(MISRA-C:2004 10.1/R) The value of an expression of integer type shall not be implicitly converted to a different underlying type if it is not a conversion to a wider integer type of the same signedness

typedef enum _MyEnum { One, Two } MyEnum;
MyEnum MyVariable;

int foo(void)
{
    int result = 1;

    if (One == MyVariable)  // fails here with MISRA-C:2004 10.1/R
    {
        result = 2;
    }    
    return result;
}

  • Why is the logical expression converted?
  • What is converted here?
  • Why does the code pass the MISRA check, when I swap One and MyVariable?

Edit: The compiler is a TI "MSP430 C/C++ Compiler v4.0.0" with included MISRA rules check.

I would suspect a compiler bug. What compiler are you using? This post mentions a compiler bug causing Misra 10.1/R failures when using TI's compiler.